Skip to content

Commit

Permalink
Placeholders for GroupProperty example
Browse files Browse the repository at this point in the history
  • Loading branch information
gpospelov committed Feb 9, 2021
1 parent 6390e27 commit 450b16c
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 7 deletions.
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ if (MVVM_BUILD_EXAMPLES)
add_subdirectory(collidingmice)
add_subdirectory(concurrentplot)
add_subdirectory(dragandmove)
add_subdirectory(groupproperty)
add_subdirectory(flateditor)
add_subdirectory(graphicsproxy)
add_subdirectory(helloworld)
Expand Down
11 changes: 11 additions & 0 deletions examples/groupproperty/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.14)
project(groupproperty_example VERSION 0.0.1 LANGUAGES CXX)

find_package(MVVM REQUIRED)

set(executable_name groupproperty)
add_subdirectory(grouppropertycore)
add_executable(${executable_name} main.cpp)

target_link_libraries(${executable_name} grouppropertycore Qt5::Widgets)
target_include_directories(${executable_name} PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>)
2 changes: 2 additions & 0 deletions examples/groupproperty/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Example "groupproperty".

11 changes: 11 additions & 0 deletions examples/groupproperty/grouppropertycore/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
set(library_name grouppropertycore)

set(CMAKE_AUTOMOC ON)

add_library(${library_name} STATIC "")
target_link_libraries(${library_name} PRIVATE MVVM::View)

target_sources(${library_name} PRIVATE
mainwindow.cpp
mainwindow.h
)
60 changes: 60 additions & 0 deletions examples/groupproperty/grouppropertycore/mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// ************************************************************************** //
//
// Model-view-view-model framework for large GUI applications
//
//! @license GNU General Public License v3 or higher (see COPYING)
//! @authors see AUTHORS
//
// ************************************************************************** //

#include "mainwindow.h"
#include <QCoreApplication>
#include <QSettings>

namespace {
const QString main_window_group = "MainWindow";
const QString size_key = "size";
const QString pos_key = "pos";
} // namespace

namespace GroupProperty {

MainWindow::MainWindow()
{
setCentralWidget(new QWidget);
initApplication();
}

MainWindow::~MainWindow() = default;

void MainWindow::closeEvent(QCloseEvent* event)
{
writeSettings();
QMainWindow::closeEvent(event);
}

void MainWindow::initApplication()
{
QCoreApplication::setApplicationName("groupproperty");
QCoreApplication::setApplicationVersion("0.1");
QCoreApplication::setOrganizationName("qt-mvvm");

QSettings settings;
if (settings.childGroups().contains(main_window_group)) {
settings.beginGroup(main_window_group);
resize(settings.value(size_key, QSize(400, 400)).toSize());
move(settings.value(pos_key, QPoint(200, 200)).toPoint());
settings.endGroup();
}
}

void MainWindow::writeSettings()
{
QSettings settings;
settings.beginGroup(main_window_group);
settings.setValue(size_key, size());
settings.setValue(pos_key, pos());
settings.endGroup();
}

} // namespace GroupProperty
37 changes: 37 additions & 0 deletions examples/groupproperty/grouppropertycore/mainwindow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// ************************************************************************** //
//
// Model-view-view-model framework for large GUI applications
//
//! @license GNU General Public License v3 or higher (see COPYING)
//! @authors see AUTHORS
//
// ************************************************************************** //

#ifndef PLOTGRAPHSCORE_MAINWINDOW_H
#define PLOTGRAPHSCORE_MAINWINDOW_H

#include <QMainWindow>
#include <memory>

namespace GroupProperty {

//! The main window of this application.

class MainWindow : public QMainWindow {
Q_OBJECT

public:
MainWindow();
~MainWindow();

protected:
void closeEvent(QCloseEvent* event);

private:
void initApplication();
void writeSettings();
};

} // namespace PlotGraphs

#endif // PLOTGRAPHSCORE_MAINWINDOW_H
24 changes: 24 additions & 0 deletions examples/groupproperty/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// ************************************************************************** //
//
// Model-view-view-model framework for large GUI applications
//
//! @license GNU General Public License v3 or higher (see COPYING)
//! @authors see AUTHORS
//
// ************************************************************************** //

#include "grouppropertycore/mainwindow.h"
#include <QApplication>
#include <QLocale>

int main(int argc, char** argv)
{
QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates));

QApplication app(argc, argv);

GroupProperty::MainWindow win;
win.show();

return app.exec();
}
6 changes: 3 additions & 3 deletions source/libmvvm_model/mvvm/model/groupitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ GroupItem::~GroupItem() = default;

GroupItem::GroupItem(model_type modelType)
: SessionItem(std::move(modelType))
, m_default_selected_index(0)
, m_index_to_select(0)
{
registerTag(TagInfo::universalTag(T_GROUP_ITEMS), /*set_as_default*/ true);
setData(ComboProperty());
Expand Down Expand Up @@ -89,7 +89,7 @@ bool GroupItem::isValidIndex() const
void GroupItem::updateCombo()
{
ComboProperty combo;
combo.setValues(m_item_labels);
combo.setCurrentIndex(m_default_selected_index);
combo.setValues(m_item_text);
combo.setCurrentIndex(m_index_to_select);
setData(combo, ItemDataRole::DATA);
}
8 changes: 4 additions & 4 deletions source/libmvvm_model/mvvm/model/groupitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ class MVVM_MODEL_EXPORT GroupItem : public SessionItem {
template <typename T> void addToGroup(const std::string& text, bool make_selected = false);
void updateCombo();

int m_default_selected_index;
std::vector<std::string> m_item_labels;
int m_index_to_select;
std::vector<std::string> m_item_text;
};

//! Adds an item of a given type to the group.
//! @param 'text' defines a text to be shown in ComboEditor when selecting an item in a group.
//! @param make_selected defines whether the item should be selected by default.
template <typename T> void GroupItem::addToGroup(const std::string& text, bool make_selected)
{
m_item_labels.push_back(text);
m_item_text.push_back(text);
insertItem(new T, TagRow::append(T_GROUP_ITEMS));
if (make_selected)
m_default_selected_index = m_item_labels.size() - 1;
m_index_to_select = m_item_text.size() - 1;
updateCombo();
}

Expand Down

0 comments on commit 450b16c

Please sign in to comment.