Giter Site home page Giter Site logo

gpospelov / qt-mvvm Goto Github PK

View Code? Open in Web Editor NEW
275.0 18.0 79.0 33.06 MB

Model View ViewModel framework for large Qt C++ applications

License: GNU General Public License v3.0

CMake 3.26% C++ 96.49% C 0.26%
qt-applications viewmodel-framework qt5 mvvm cmake multi-platform node-editor property-editor

qt-mvvm's Introduction

Model View ViewModel framework for large C++ Qt applications

Linux Windows Mac OS version codecov

Overview

This model-view-viewmodel framework is intended for large Qt based scientific applications written in C++. Project was created as a playground toward GUI refactoring of BornAgain project.

Main features of the framework are:

  • Application model to store arbitrary data of GUI session.
  • Serialization of application models to json.
  • Undo/redo based on command pattern.
  • View model to show parts of application model in Qt widgets. Depends on Qt.
  • Scientific plotting based on qcustomplot.
  • Automatic generation of widgets from model content.
  • Property editors.
  • Flexible layout of Qt's trees and tables.

User examples

Requirements

  • C++-17
  • CMake 3.14
  • Qt 5.12

Installation instructions

git clone --recurse-submodules  https://github.com/gpospelov/qt-mvvm.git
mkdir <build-dir>; cd <build-dir>
cmake <source>; make -j8; ctest

# run one of examples
<build-dir>/bin/collidingmice

Example

If you are familiar with Qt's reach example section you might saw it's funny collidingmice example showing basics of QGraphicsScene.

Example collidingmice

To demonstrate the idea behind qt-mvvm library the code of the example was slightly modified. The mice data has been moved into the dedicated model, the content of the model was shown both in the QGraphicsScene and in QTreeView. It is possible now to save the application state in json file and later load the session back with saved mice positions. Additionally, it is possible to go back in time and watch how mice are moving in opposite directions by dragging a slider:

Example collidingmice

See short animation here.

The demo shows that qt-mvvm library allows to equip the GUI with the serialization and undo/redo and to provide proper model/view relations via relatively small modifications to the original code. Implementing similar features from the scratch in bare metal Qt would take much more time and the resulting code wouldn't be easily transferable to another project.

This and other examples can be found in examples sub-directory of qt-mvvm package.

The context

Qt, naturally, has model view to manage the data and its presentation. According to some, Qt is misusing the model view terminology and more correct naming would be

  • Data -> Model
  • Model -> ViewModel
  • Delegate -> Controller
  • View (that's ok)

Whatever the right terminology is, Qt doesn't tell much about the architecture of complex applications, how to structure the Data and where to put business logic. Qt's model view is rather a way to adapt the user data for Qt's trees and tables and adjust their behavior with delegates.

Given library is an attempt to understand how to deal with the application data.

More explanations

The framework consists of three libraries: libmvvm_model.so, libmvvm_viewmodel.so, and libmvvm_view.so.

libmvvm_model.so defines tree-like structure SessionModel to store any data of the GUI session. This part of the framework is intentionally made Qt-independent. The idea behind is the following:

In large Qt applications, the business logic gets quickly spoiled with presentation logic. Qt classes like QModelIndex start to appear everywhere, even in places that have nothing to do with Qt graphics. Attempt to store GUI session data in QAbstractItemModel leads to inflexible layout in Qt trees and tables. Attempt to fix this with QAbstractProxyModel leads to the appearance of overwhelmingly complicated proxy models. Removing Qt from dependencies allows focusing more on common needs (i.e. objects construction, property editing, etc) of GUI applications rather than on presentation details.

SessionModel has a concept of properties, undo/redo, serialization, and it's own minimal signaling to handle business logic. Thus, the intention here is to build an application model to handle data and logic of GUI while being independent on any particular GUI library.

Strictly speaking, libmvvm_model.so still relies on QVariant but eventually, it will be replaced with std::variant.

Second library, libmmv_viewmodel.so, contains ViewModel and serves as a thin counterpart of SessionModel in the Qt world. ViewModel doesn't own the data but simply acts as a proxy to different parts of SessionModel. It is derived from QAbstractItemModel and intended to work together with Qt's trees and tables. The layout of ViewModel (i.e. parent/child relationships) doesn't follow the layout of the original SessionModel. It is generated on the fly by using strategy who-is-my-next-child provided by the user. In practice, it allows generating Qt tables and trees with arbitrary layouts, based on a common data source, without diving into the nightmare of QAbstractProxyModel. Particularly, the aforementioned machinery allows having something in the line of the ancient Qt property browser framework.

Flat editor example

Third library, libmmv_view.so, contains few widgets for plotting and property editing.

Size of the framework

  • 20k loc of libraries (libmvvm_model.so, libmmv_viewmodel.so and libmmv_view.so)
  • 15k loc of tests
  • 10k of user examples

Disclaimer and afterword

The library is intended for large GUI applications. The definition of large is quite arbitrary and means something in the range 20k - 200k lines of code. The main logic here is that using the additional library for smaller Qt applications is redundant, Qt has everything that may be required. If small GUI becomes messy with time, it can always be refactored or even rewritten from scratch.

However, when the number of views to show the same data is getting large, and the GUI enters the range 20k - 200k, this is were a given library might help in proper separation of data, logic, and UI. When the GUI grows even further, well, developers of such large GUI know already what they need and probably have already implemented similar machinery.

The project is under active development.

qt-mvvm's People

Contributors

alexanderschober avatar andrewsazonov avatar dmitryyurov avatar gpospelov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

qt-mvvm's Issues

Introduce ViewModelBuilder

  • Make setter for ViewModel in ViewModelController
  • Move all ViewModel standard factory methods in dedicated folder
  • Introduce ViewModelBuilder or generic CreateViewModel
  • Cleanup LayerEditor example
  • Consider templated functions
CreateViewModel<AllItemsStrategy, NameLabelStrategy>(session_model)

Doxygen comments: uniform C++ style

Prefer C++ style (//!) over C style (/*!).

Omit @class lines in fornt of class definitions.

Omit @brief tag where not specially required.

Consider alternative names instead of "viewmodel"

How to maximize the cognitive load of keeping a concept X apart from the related concepts "model" and "view"? Right, choose a name like X="view model".

There may be good theoretical grounds for the term "view model", but for API users and library codevelopers it is about the worst possible choice.

Wikipedia says "The view model has been described as a state of the data in the model." What then about the term "state"? Other suggestions?

missing CMakeLists.txt in thirdparty/gtest/googletest

cmake fails with message:

CMake Error at thirdparty/gtest/CMakeLists.txt:10 (add_subdirectory):
  The source directory

    /G/tmp/qt-mvvm/thirdparty/gtest/googletest

  does not contain a CMakeLists.txt file.


-- Configuring incomplete, errors occurred!

DataLoader minor fixes, part II

  • Instead of two vertical toolbars with buttons make one horizontal (up to @AlexanderSchober)
  • Provide a working selector "import as a new collection", or "add to current collection"
  • Add two different test ASCII files somewhere to the root of quickrefl example. It is handy to have something in hands to try loader. You can generate them with BornAgain (up to @AlexanderSchober).
  • After loading a new graph, make corresponding viewport selected so user already sees the plot.

Create WelcomeView

  • Check saving in "mouse" example
  • Prevent closing of app on unsaved dialog
  • Provide modelLoaded signal
  • Recent projects
  • Application Actions
  • Menu File
  • Implement scroll bar for RecentProjectsWidget
  • Implement Logo
  • Make labels instead of buttons
  • Implement DaRefl version number
  • Implement exit in file menu
  • Implement recent-projects in a menu

Completed in gpospelov/DaRefl#4

Empty directory thirdparty/gtest/googletest

Similar issue #3 which was closed but not resolved.

CMake fails to build:

$ cmake ..
-- The CXX compiler identification is GNU 9.2.1
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
--   Qt5::Widgets /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5.12.5
--   Includes: /usr/include/x86_64-linux-gnu/qt5/;/usr/include/x86_64-linux-gnu/qt5/QtWidgets;/usr/include/x86_64-linux-gnu/qt5/QtGui;/usr/include/x86_64-linux-gnu/qt5/QtCore;/usr/lib/x86_64-linux-gnu/qt5//mkspecs/linux-g++;/usr/include
CMake Error at thirdparty/gtest/CMakeLists.txt:10 (add_subdirectory):
  The source directory

    /G/ext/qt-mvvm/thirdparty/gtest/googletest

  does not contain a CMakeLists.txt file.


-- Configuring incomplete, errors occurred!

Actually, in my fresh clone the directory thirdparty/gtest/googletest is empty.

In the Github source browser, I do see a link to the upstream googletest repository. This link, however, seems to be ignored by git clone on my Debian/testing system.

Make QML example

  • Introduce backend and register it in QML
  • Provide property editor with group property

Make package installable

  • Make all libraries installablle
  • Provide generation of MVVMConfig.cmake and MVVMTargets.cmake
  • Make qt-mvvm accessible via find_package
  • Make all examples installable.
  • Make all examples compilable as a standalone projects
  • Provides so versions

Visual Studio 2019 compiler crash on trying to compile tests

With the following message:

C:\Users\DmitryYurov\Documents\Software\qt-mvvm\Source\tests\viewmodel\TestAxisPlotControllers.cpp : fatal error C1001: An internal error has occurred in the compiler.
(compiler file 'd:\agent_work\3\s\src\vctools\Compiler\Utc\src\p2\main.c', line 160)
To work around this problem, try simplifying or changing the program near the locations listed above.
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
Internal Compiler Error in C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.22.27905\bin\HostX86\x64\CL.exe. You will be prompted to send an error report to Microsoft later.
INTERNAL COMPILER ERROR in 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.22.27905\bin\HostX86\x64\CL.exe'
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information

Create a library `libmvvm_view`

  • Introduce new library libmvvm_widgets
  • Move all custom trees, plotting there.
  • Make libmvvm_viewmodel containing only Qt-related view models

Add tooltip to SessionItem

  • Add TOOLTIP data role
  • Add tolltip get/set to SessionItem
  • Provide filtering of tooltip data during serialization
  • Use tooltips from memory during de-serialization
  • Forward tooltip to ViewModel

Add two splitters for ImportWindow

Should separate tree with graphs, from the plot, and from plot properties. Consider saving relative splitter positions in QSettings. Use WelcomeViewSettings as an example

DataLoader minor fixes

  • Use QSettings as in WelcomeViewSettings to rely on common config.
  • Change icons to material design
  • Make QSplitter splitting on equal half between "Selected Files" and "Text Selection Rules"
  • Enable selector "Merge into one dataset".
  • Take care of FIXME marks

Finish DataLoader

  • Column settings should be saved in QSettings
  • Fix data column names not being utilized
  • Manage NaN values in the imported data
  • Add possibility to ignore columns for import
  • Fixed the QDialog issue

Split the project

  • Create DaRefl repository
  • Move DaRefl
  • Enable testing
  • Attach qt-mvv as a submodule
  • Make directory structure flat
  • Update header guards
  • Update project source file header
  • Add AUTHORS
  • Beautify the code
  • Enable continuos integration

Directory structure: Shorten source tree

For command-line addicts, source/libmvvm_viewmodel/mvvm/ is painful. Also, I see no good reason for such verbosity.

What about

lib/model
lib/state

(state to replace viewmodel as per #50)

Or create two libraries, and have an even flatter directory structure

libmodel
libstate

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.