Kaynağa Gözat

Make separate qttools target common for samples

Viktor Kopp 5 yıl önce
ebeveyn
işleme
f8ec65b184

+ 1 - 4
CMakeLists.txt

@@ -117,8 +117,5 @@ if(MAKE_TESTS)
 endif()
 
 if(MAKE_EXAMPLES)
-    add_subdirectory("examples/addressbook")
-    add_subdirectory("examples/addressbookserver")
-    add_subdirectory("examples/simplechat")
-    add_subdirectory("examples/simplechatserver")
+    add_subdirectory("examples")
 endif()

+ 5 - 0
examples/CMakeLists.txt

@@ -0,0 +1,5 @@
+add_subdirectory("addressbook")
+add_subdirectory("addressbookserver")
+add_subdirectory("simplechat")
+add_subdirectory("simplechatserver")
+add_subdirectory("qttools")

+ 3 - 5
examples/addressbook/CMakeLists.txt

@@ -29,13 +29,11 @@ generate_qtprotobuf(TARGET ${TARGET}
     GENERATED_HEADERS ${GENERATED_HEADERS})
 
 file(GLOB SOURCES main.cpp
-    addressbookengine.cpp
-    universallistmodel.cpp
-    universallistmodelbase.cpp)
+    addressbookengine.cpp)
 
 file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../addressbookserver/cert.pem DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
 
 add_executable(${TARGET} ${SOURCES} resources.qrc)
-add_dependencies(${TARGET} ${QtProtobuf_GENERATED})
+add_dependencies(${TARGET} ${QtProtobuf_GENERATED} qttools)
 target_include_directories(${TARGET} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/generated)
-target_link_libraries(${TARGET} QtProtobufProject::QtProtobuf QtProtobufProject::QtGrpc ${QtProtobuf_GENERATED} Qt5::Quick Qt5::Qml)
+target_link_libraries(${TARGET} qttools QtProtobufProject::QtProtobuf QtProtobufProject::QtGrpc ${QtProtobuf_GENERATED} Qt5::Quick Qt5::Qml)

+ 0 - 24
examples/addressbook/universallistmodelbase.h

@@ -1,24 +0,0 @@
-#pragma once
-
-#include <QAbstractListModel>
-/*!
- * \brief The UniversalListModelBase class to make prossible properties definition for UniversalListModel
- * This class should not be used as is, but leaves this possibility.
- */
-class UniversalListModelBase : public QAbstractListModel
-{
-    Q_OBJECT
-    Q_PROPERTY(int count READ count NOTIFY countChanged)
-public:
-    explicit UniversalListModelBase(QObject *parent = nullptr);
-
-    /*!
-     * \brief count property that declares row count of UniversalListModel
-     * \return
-     */
-    virtual int count() const = 0;
-    Q_INVOKABLE virtual void remove(int valueIndex) = 0;
-
-signals:
-    void countChanged();
-};

+ 19 - 0
examples/qttools/CMakeLists.txt

@@ -0,0 +1,19 @@
+set(CMAKE_INCLUDE_CURRENT_DIR ON)
+set(CMAKE_AUTOMOC ON)
+set(CMAKE_AUTORCC ON)
+
+file(GLOB HEADERS
+    universallistmodelbase.h
+    universallistmodel.h
+)
+
+file(GLOB SOURCES
+    universallistmodelbase.cpp
+    universallistmodel.cpp
+)
+
+# headers added to make them visible in IDE
+add_library(qttools ${SOURCES} ${HEADERS})
+
+target_include_directories(qttools PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
+target_link_libraries(qttools Qt5::Core)

+ 0 - 0
examples/addressbook/universallistmodel.cpp → examples/qttools/universallistmodel.cpp


+ 0 - 0
examples/addressbook/universallistmodel.h → examples/qttools/universallistmodel.h


+ 0 - 0
examples/addressbook/universallistmodelbase.cpp → examples/qttools/universallistmodelbase.cpp


+ 1 - 1
examples/simplechat/universallistmodelbase.h → examples/qttools/universallistmodelbase.h

@@ -2,7 +2,7 @@
 
 #include <QAbstractListModel>
 /*!
- * \brief The UniversalListModelBase class to make prossible properties definition for UniversalListModel
+ * \brief The UniversalListModelBase class to make possible properties definition for UniversalListModel
  * This class should not be used as is, but leaves this possibility.
  */
 class UniversalListModelBase : public QAbstractListModel

+ 3 - 4
examples/simplechat/CMakeLists.txt

@@ -25,12 +25,11 @@ generate_qtprotobuf(TARGET ${TARGET}
 
 file(GLOB SOURCES main.cpp
     simplechatengine.cpp
-    universallistmodelbase.cpp
-    universallistmodel.cpp)
+)
 
 file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../simplechatserver/cert.pem DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
 
 add_executable(${TARGET} ${SOURCES} resources.qrc)
-add_dependencies(${TARGET} ${QtProtobuf_GENERATED})
+add_dependencies(${TARGET} ${QtProtobuf_GENERATED} qttools)
 target_include_directories(${TARGET} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/generated)
-target_link_libraries(${TARGET}  QtProtobufProject::QtProtobuf QtProtobufProject::QtGrpc ${QtProtobuf_GENERATED} Qt5::Quick Qt5::Qml)
+target_link_libraries(${TARGET} qttools QtProtobufProject::QtProtobuf QtProtobufProject::QtGrpc ${QtProtobuf_GENERATED} Qt5::Quick Qt5::Qml)

+ 0 - 1
examples/simplechat/universallistmodel.cpp

@@ -1 +0,0 @@
-#include "universallistmodel.h"

+ 0 - 203
examples/simplechat/universallistmodel.h

@@ -1,203 +0,0 @@
-#pragma once
-
-#include <universallistmodelbase.h>
-
-#include <QObject>
-#include <QList>
-#include <QHash>
-#include <QSharedPointer>
-#include <QMetaProperty>
-#include <QMetaObject>
-
-#include <QDebug>
-
-
-/*!
- * \brief Universal list model is QObject-base list model abstraction.
- * It exposes all objects properties as data-roles.
- */
-template <typename T>
-class UniversalListModel : public UniversalListModelBase
-{
-public:
-    UniversalListModel(QList<QSharedPointer<T>> container = {}, QObject* parent = 0) : UniversalListModelBase(parent) {
-        reset(container);
-    }
-    ~UniversalListModel() {
-        clear();
-    }
-
-    int rowCount(const QModelIndex &parent) const override {
-        Q_UNUSED(parent)
-        return count();
-    }
-
-    int count() const override {
-        return m_container.count();
-    }
-
-    QHash<int, QByteArray> roleNames() const override {
-        if(s_roleNames.isEmpty()) {
-            int propertyCount = T::staticMetaObject.propertyCount();
-            for(int i = 1; i < propertyCount; i++) {
-                s_roleNames.insert(Qt::UserRole + i, T::staticMetaObject.property(i).name());
-            }
-            s_roleNames[propertyCount] = "modelData";
-        }
-        return s_roleNames;
-    }
-
-    QVariant data(const QModelIndex &index, int role) const override
-    {
-        int row = index.row();
-
-        if(row < 0 || row >= m_container.count() || m_container.at(row).isNull()) {
-            return QVariant();
-        }
-
-        T* dataPtr = m_container.at(row).data();
-
-        if(s_roleNames.value(role) == "modelData") {
-            return QVariant::fromValue(dataPtr);
-        }
-        return dataPtr->property(s_roleNames.value(role));
-    }
-
-    /*!
-     * \brief append
-     * \param value
-     * \return
-     */
-    int append(T* value) {
-        Q_ASSERT_X(value != nullptr, fullTemplateName(), "Trying to add member of NULL");
-
-        if(m_container.indexOf(value) >= 0) {
-#ifdef DEBUG
-            qDebug() << fullTemplateName() << "Member already exists";
-#endif
-            return -1;
-        }
-        beginInsertRows(QModelIndex(), m_container.count(), m_container.count());
-        m_container.append(QSharedPointer<T>(value));
-        emit countChanged();
-        endInsertRows();
-        return m_container.count() - 1;
-    }
-
-    /*!
-     * \brief prepend
-     * \param value
-     * \return
-     */
-    int prepend(T* value) {
-        Q_ASSERT_X(value != nullptr, fullTemplateName(), "Trying to add member of NULL");
-
-        if(m_container.indexOf(value) >= 0) {
-#ifdef DEBUG
-            qDebug() << fullTemplateName() << "Member already exists";
-#endif
-            return -1;
-        }
-        beginInsertRows(QModelIndex(), 0, 0);
-        m_container.prepend(QSharedPointer<T>(value));
-        emit countChanged();
-        endInsertRows();
-        return 0;
-    }
-
-    /*!
-     * \brief remove
-     * \param value
-     */
-    void remove(T* value) {
-        Q_ASSERT_X(value != nullptr, fullTemplateName(), ": Trying to remove member of NULL");
-
-        int valueIndex = m_container.indexOf(value);
-
-        remove(valueIndex);
-    }
-
-    void remove(int valueIndex) override {
-        if(valueIndex >= 0) {
-            beginRemoveRows(QModelIndex(), valueIndex, valueIndex);
-            m_container.removeAt(valueIndex);
-            emit countChanged();
-            endRemoveRows();
-        }
-    }
-
-    /*!
-     * \brief Resets container with new container passed as parameter
-     * \param container a data for model. Should contain QSharedPointer's to objects.
-     * Passing empty container makes model empty. This method should be used to cleanup model.
-     */
-    void reset(const QList<QSharedPointer<T> >& container) {
-        beginResetModel();
-        clear();
-        m_container = container;
-        emit countChanged();
-        endResetModel();
-    }
-
-    /*!
-     * \brief Returns the item at index position i in the list. i must be a valid index position in the list (i.e., 0 <= i < rowCount()).
-     * This function is very fast (constant time).
-     * \param i index of looking object
-     * \return Object at provided index
-     */
-    T* at(int i) const {
-        return m_container.at(i);
-    }
-
-    /*!
-     * \brief Looking for index of objec
-     * \param value
-     * \return
-     */
-    int indexOf(T* value) const {
-        return m_container.indexOf(value);
-    }
-
-    /*!
-     * \brief findByProperty method finds item in internal container property of that is provided value
-     * \param propertyName Latin1 name of looking property
-     * \param value property of corresponded type inside QVariant container
-     * \return
-     */
-    QSharedPointer<T> findByProperty(const char* propertyName, const QVariant& value) const {
-        auto iter = std::find_if(m_container.begin(), m_container.end(), [=](const QSharedPointer<T> &item) -> bool {
-            return item->property(propertyName) == value;
-        });
-
-        if(iter != m_container.end()) {
-            return *iter;
-        }
-        return QSharedPointer<T>();
-    }
-
-    /*!
-     * \brief container returns internal container
-     * \return
-     */
-    QList<QSharedPointer<T> > container() const {
-        return m_container;
-    }
-
-protected:
-    void clear() {
-        m_container.clear();
-        emit countChanged();
-    }
-
-    QList<QSharedPointer<T> > m_container;
-    static QHash<int, QByteArray> s_roleNames;
-
-
-private:
-    static QByteArray fullTemplateName() { //Debug helper
-        return QString("UniversalListModel<%1>").arg(T::staticMetaObject.className()).toLatin1();
-    }
-};
-
-template<typename T>
-QHash<int, QByteArray> UniversalListModel<T>::s_roleNames;

+ 0 - 6
examples/simplechat/universallistmodelbase.cpp

@@ -1,6 +0,0 @@
-#include "universallistmodelbase.h"
-
-UniversalListModelBase::UniversalListModelBase(QObject *parent) : QAbstractListModel(parent)
-{
-
-}