From c75563c184de28aca46ae2cfb52a4318771cfa1e Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Tue, 11 Dec 2012 15:35:47 +0100 Subject: [PATCH] report model and view --- apps/opencs/CMakeLists.txt | 8 ++- apps/opencs/model/doc/document.cpp | 10 +++- apps/opencs/model/doc/document.hpp | 7 ++- apps/opencs/model/tools/reportmodel.cpp | 66 ++++++++++++++++++++++++ apps/opencs/model/tools/reportmodel.hpp | 35 +++++++++++++ apps/opencs/model/tools/tools.cpp | 28 +++++++--- apps/opencs/model/tools/tools.hpp | 13 ++++- apps/opencs/model/world/idtable.cpp | 2 +- apps/opencs/model/world/universalid.cpp | 3 +- apps/opencs/model/world/universalid.hpp | 5 +- apps/opencs/view/doc/view.cpp | 5 +- apps/opencs/view/tools/reportsubview.cpp | 25 +++++++++ apps/opencs/view/tools/reportsubview.hpp | 29 +++++++++++ apps/opencs/view/tools/subviews.cpp | 12 +++++ apps/opencs/view/tools/subviews.hpp | 14 +++++ 15 files changed, 246 insertions(+), 16 deletions(-) create mode 100644 apps/opencs/model/tools/reportmodel.cpp create mode 100644 apps/opencs/model/tools/reportmodel.hpp create mode 100644 apps/opencs/view/tools/reportsubview.cpp create mode 100644 apps/opencs/view/tools/reportsubview.hpp create mode 100644 apps/opencs/view/tools/subviews.cpp create mode 100644 apps/opencs/view/tools/subviews.hpp diff --git a/apps/opencs/CMakeLists.txt b/apps/opencs/CMakeLists.txt index 67cbcc772a..1f9fe85a24 100644 --- a/apps/opencs/CMakeLists.txt +++ b/apps/opencs/CMakeLists.txt @@ -8,12 +8,14 @@ set (OPENCS_SRC model/world/commands.cpp model/world/idtableproxymodel.cpp model/world/record.cpp model/tools/tools.cpp model/tools/operation.cpp model/tools/stage.cpp model/tools/verifier.cpp - model/tools/mandatoryid.cpp + model/tools/mandatoryid.cpp model/tools/reportmodel.cpp view/doc/viewmanager.cpp view/doc/view.cpp view/doc/operations.cpp view/doc/operation.cpp view/doc/subviewfactory.cpp view/doc/subview.cpp view/world/table.cpp view/world/tablesubview.cpp view/world/subviews.cpp + + view/tools/reportsubview.cpp view/tools/subviews.cpp ) set (OPENCS_HDR @@ -26,12 +28,14 @@ set (OPENCS_HDR model/world/commands.hpp model/tools/tools.hpp model/tools/operation.hpp model/tools/stage.hpp model/tools/verifier.hpp - model/tools/mandatoryid.hpp + model/tools/mandatoryid.hpp model/tools/reportmodel.hpp view/doc/viewmanager.hpp view/doc/view.hpp view/doc/operations.hpp view/doc/operation.hpp view/doc/subviewfactory.hpp view/doc/subview.hpp view/doc/subviewfactoryimp.hpp view/world/table.hpp view/world/tablesubview.hpp view/world/subviews.hpp + + view/tools/reportsubview.hpp view/tools/subviews.hpp ) set (OPENCS_US diff --git a/apps/opencs/model/doc/document.cpp b/apps/opencs/model/doc/document.cpp index f03344e08a..9d26944832 100644 --- a/apps/opencs/model/doc/document.cpp +++ b/apps/opencs/model/doc/document.cpp @@ -50,10 +50,11 @@ void CSMDoc::Document::save() emit progress (1, 16, State_Saving, 1, this); } -void CSMDoc::Document::verify() +CSMWorld::UniversalId CSMDoc::Document::verify() { - mTools.runVerifier(); + CSMWorld::UniversalId id = mTools.runVerifier(); emit stateChanged (getState(), this); + return id; } void CSMDoc::Document::abortOperation (int type) @@ -102,6 +103,11 @@ CSMWorld::Data& CSMDoc::Document::getData() return mData; } +CSMTools::ReportModel *CSMDoc::Document::getReport (const CSMWorld::UniversalId& id) +{ + return mTools.getReport (id); +} + void CSMDoc::Document::progress (int current, int max, int type) { emit progress (current, max, type, 1, this); diff --git a/apps/opencs/model/doc/document.hpp b/apps/opencs/model/doc/document.hpp index f9d0a232ce..43e8bba37b 100644 --- a/apps/opencs/model/doc/document.hpp +++ b/apps/opencs/model/doc/document.hpp @@ -13,6 +13,8 @@ #include "state.hpp" +class QAbstractItemModel; + namespace CSMDoc { class Document : public QObject @@ -50,7 +52,7 @@ namespace CSMDoc void save(); - void verify(); + CSMWorld::UniversalId verify(); void abortOperation (int type); @@ -58,6 +60,9 @@ namespace CSMDoc CSMWorld::Data& getData(); + CSMTools::ReportModel *getReport (const CSMWorld::UniversalId& id); + ///< The ownership of the returned report is not transferred. + signals: void stateChanged (int state, CSMDoc::Document *document); diff --git a/apps/opencs/model/tools/reportmodel.cpp b/apps/opencs/model/tools/reportmodel.cpp new file mode 100644 index 0000000000..f142d76697 --- /dev/null +++ b/apps/opencs/model/tools/reportmodel.cpp @@ -0,0 +1,66 @@ + +#include "reportmodel.hpp" + +#include + +int CSMTools::ReportModel::rowCount (const QModelIndex & parent) const +{ + if (parent.isValid()) + return 0; + + return mRows.size(); +} + +int CSMTools::ReportModel::columnCount (const QModelIndex & parent) const +{ + if (parent.isValid()) + return 0; + + return 2; +} + +QVariant CSMTools::ReportModel::data (const QModelIndex & index, int role) const +{ + if (role!=Qt::DisplayRole) + return QVariant(); + + if (index.column()==0) + return static_cast (mRows.at (index.row()).first.getType()); + else + return mRows.at (index.row()).second.c_str(); +} + +QVariant CSMTools::ReportModel::headerData (int section, Qt::Orientation orientation, int role) const +{ + if (role!=Qt::DisplayRole) + return QVariant(); + + if (orientation==Qt::Vertical) + return QVariant(); + + return tr (section==0 ? "Type" : "Description"); +} + +bool CSMTools::ReportModel::removeRows (int row, int count, const QModelIndex& parent) +{ + if (parent.isValid()) + return false; + + mRows.erase (mRows.begin()+row, mRows.begin()+row+count); + + return true; +} + +void CSMTools::ReportModel::add (const std::string& row) +{ + std::string::size_type index = row.find ('|'); + + if (index==std::string::npos) + throw std::logic_error ("invalid report message"); + + beginInsertRows (QModelIndex(), mRows.size(), mRows.size()); + + mRows.push_back (std::make_pair (row.substr (0, index), row.substr (index+1))); + + endInsertRows(); +} \ No newline at end of file diff --git a/apps/opencs/model/tools/reportmodel.hpp b/apps/opencs/model/tools/reportmodel.hpp new file mode 100644 index 0000000000..8814b6d497 --- /dev/null +++ b/apps/opencs/model/tools/reportmodel.hpp @@ -0,0 +1,35 @@ +#ifndef CSM_TOOLS_REPORTMODEL_H +#define CSM_TOOLS_REPORTMODEL_H + +#include +#include + +#include + +#include "../world/universalid.hpp" + +namespace CSMTools +{ + class ReportModel : public QAbstractTableModel + { + Q_OBJECT + + std::vector > mRows; + + public: + + virtual int rowCount (const QModelIndex & parent = QModelIndex()) const; + + virtual int columnCount (const QModelIndex & parent = QModelIndex()) const; + + virtual QVariant data (const QModelIndex & index, int role = Qt::DisplayRole) const; + + virtual QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + + virtual bool removeRows (int row, int count, const QModelIndex& parent = QModelIndex()); + + void add (const std::string& row); + }; +} + +#endif diff --git a/apps/opencs/model/tools/tools.cpp b/apps/opencs/model/tools/tools.cpp index a3cdb78220..8dd1c0fe6f 100644 --- a/apps/opencs/model/tools/tools.cpp +++ b/apps/opencs/model/tools/tools.cpp @@ -8,7 +8,9 @@ #include "../doc/state.hpp" #include "../world/data.hpp" +#include "../world/universalid.hpp" +#include "reportmodel.hpp" #include "mandatoryid.hpp" CSMTools::Operation *CSMTools::Tools::get (int type) @@ -54,9 +56,10 @@ CSMTools::Verifier *CSMTools::Tools::getVerifier() return mVerifier; } -CSMTools::Tools::Tools (CSMWorld::Data& data) : mData (data), mVerifier (0) +CSMTools::Tools::Tools (CSMWorld::Data& data) : mData (data), mVerifier (0), mNextReportNumber (0) { - + for (std::map::iterator iter (mReports.begin()); iter!=mReports.end(); ++iter) + delete iter->second; } CSMTools::Tools::~Tools() @@ -64,9 +67,14 @@ CSMTools::Tools::~Tools() delete mVerifier; } -void CSMTools::Tools::runVerifier() +CSMWorld::UniversalId CSMTools::Tools::runVerifier() { + mReports.insert (std::make_pair (mNextReportNumber++, new ReportModel)); + mActiveReports[CSMDoc::State_Verifying] = mNextReportNumber-1; + getVerifier()->start(); + + return CSMWorld::UniversalId (CSMWorld::UniversalId::Type_VerificationResults, mNextReportNumber-1); } void CSMTools::Tools::abortOperation (int type) @@ -93,15 +101,23 @@ int CSMTools::Tools::getRunningOperations() const return result; } +CSMTools::ReportModel *CSMTools::Tools::getReport (const CSMWorld::UniversalId& id) +{ + if (id.getType()!=CSMWorld::UniversalId::Type_VerificationResults) + throw std::logic_error ("invalid request for report model: " + id.toString()); + + return mReports.at (id.getIndex()); +} + void CSMTools::Tools::verifierDone() { emit done (CSMDoc::State_Verifying); } -#include void CSMTools::Tools::verifierMessage (const QString& message, int type) { - /// \todo store it in a result model instead + std::map::iterator iter = mActiveReports.find (type); - std::cout << message.toStdString() << std::endl; + if (iter!=mActiveReports.end()) + mReports[iter->second]->add (message.toStdString()); } \ No newline at end of file diff --git a/apps/opencs/model/tools/tools.hpp b/apps/opencs/model/tools/tools.hpp index da49fd8b8d..652345c6da 100644 --- a/apps/opencs/model/tools/tools.hpp +++ b/apps/opencs/model/tools/tools.hpp @@ -3,15 +3,19 @@ #include +#include + namespace CSMWorld { class Data; + class UniversalId; } namespace CSMTools { class Verifier; class Operation; + class ReportModel; class Tools : public QObject { @@ -19,6 +23,9 @@ namespace CSMTools CSMWorld::Data& mData; Verifier *mVerifier; + std::map mReports; + int mNextReportNumber; + std::map mActiveReports; // type, report number // not implemented Tools (const Tools&); @@ -38,13 +45,17 @@ namespace CSMTools virtual ~Tools(); - void runVerifier(); + CSMWorld::UniversalId runVerifier(); + ///< \return ID of the report for this verification run void abortOperation (int type); ///< \attention The operation is not aborted immediately. int getRunningOperations() const; + ReportModel *getReport (const CSMWorld::UniversalId& id); + ///< The ownership of the returned report is not transferred. + private slots: void verifierDone(); diff --git a/apps/opencs/model/world/idtable.cpp b/apps/opencs/model/world/idtable.cpp index a2b0a3c1f5..3fe1fadfef 100644 --- a/apps/opencs/model/world/idtable.cpp +++ b/apps/opencs/model/world/idtable.cpp @@ -35,7 +35,7 @@ QVariant CSMWorld::IdTable::data (const QModelIndex & index, int role) const return QVariant(); if (role==Qt::EditRole && !mIdCollection->isEditable (index.column())) - return QVariant(); + return QVariant(); return mIdCollection->getData (index.row(), index.column()); } diff --git a/apps/opencs/model/world/universalid.cpp b/apps/opencs/model/world/universalid.cpp index 98fb1d0b94..81561bcf42 100644 --- a/apps/opencs/model/world/universalid.cpp +++ b/apps/opencs/model/world/universalid.cpp @@ -30,6 +30,7 @@ namespace static const TypeData sIndexArg[] = { + { CSMWorld::UniversalId::Class_Transient, CSMWorld::UniversalId::Type_VerificationResults, "Verification Results" }, { CSMWorld::UniversalId::Class_None, CSMWorld::UniversalId::Type_None, 0 } // end marker }; @@ -39,7 +40,7 @@ CSMWorld::UniversalId::UniversalId (const std::string& universalId) { std::string::size_type index = universalId.find (':'); - if (index!=std::string::npos) + if (index==std::string::npos) { std::string type = universalId.substr (0, index); diff --git a/apps/opencs/model/world/universalid.hpp b/apps/opencs/model/world/universalid.hpp index 7b630ebc7a..14c0865105 100644 --- a/apps/opencs/model/world/universalid.hpp +++ b/apps/opencs/model/world/universalid.hpp @@ -31,7 +31,10 @@ namespace CSMWorld enum Type { Type_None, - Type_Globals + + Type_Globals, + + Type_VerificationResults }; private: diff --git a/apps/opencs/view/doc/view.cpp b/apps/opencs/view/doc/view.cpp index 47053192b0..4ffd9d7491 100644 --- a/apps/opencs/view/doc/view.cpp +++ b/apps/opencs/view/doc/view.cpp @@ -12,6 +12,8 @@ #include "../world/subviews.hpp" +#include "../tools/subviews.hpp" + #include "viewmanager.hpp" #include "operations.hpp" #include "subview.hpp" @@ -122,6 +124,7 @@ CSVDoc::View::View (ViewManager& viewManager, CSMDoc::Document *document, int to setupUi(); CSVWorld::addSubViewFactories (mSubViewFactory); + CSVTools::addSubViewFactories (mSubViewFactory); } CSVDoc::View::~View() @@ -200,7 +203,7 @@ void CSVDoc::View::save() void CSVDoc::View::verify() { - mDocument->verify(); + addSubView (mDocument->verify()); } void CSVDoc::View::addGlobalsSubView() diff --git a/apps/opencs/view/tools/reportsubview.cpp b/apps/opencs/view/tools/reportsubview.cpp new file mode 100644 index 0000000000..3c92836842 --- /dev/null +++ b/apps/opencs/view/tools/reportsubview.cpp @@ -0,0 +1,25 @@ + +#include "reportsubview.hpp" + +#include +#include + +#include "../../model/tools/reportmodel.hpp" + +CSVTools::ReportSubView::ReportSubView (const CSMWorld::UniversalId& id, CSMDoc::Document& document) +: CSVDoc::SubView (id) +{ + setWidget (mTable = new QTableView (this)); + mTable->setModel (document.getReport (id)); + + mTable->horizontalHeader()->setResizeMode (QHeaderView::Interactive); + mTable->verticalHeader()->hide(); + mTable->setSortingEnabled (true); + mTable->setSelectionBehavior (QAbstractItemView::SelectRows); + mTable->setSelectionMode (QAbstractItemView::ExtendedSelection); +} + +void CSVTools::ReportSubView::setEditLock (bool locked) +{ + // ignored. We don't change document state anyway. +} \ No newline at end of file diff --git a/apps/opencs/view/tools/reportsubview.hpp b/apps/opencs/view/tools/reportsubview.hpp new file mode 100644 index 0000000000..f81f3c386f --- /dev/null +++ b/apps/opencs/view/tools/reportsubview.hpp @@ -0,0 +1,29 @@ +#ifndef CSV_TOOLS_REPORTSUBVIEW_H +#define CSV_TOOLS_REPORTSUBVIEW_H + +#include "../doc/subview.hpp" + +class QTableView; + +namespace CSMDoc +{ + class Document; +} + +namespace CSVTools +{ + class Table; + + class ReportSubView : public CSVDoc::SubView + { + QTableView *mTable; + + public: + + ReportSubView (const CSMWorld::UniversalId& id, CSMDoc::Document& document); + + virtual void setEditLock (bool locked); + }; +} + +#endif \ No newline at end of file diff --git a/apps/opencs/view/tools/subviews.cpp b/apps/opencs/view/tools/subviews.cpp new file mode 100644 index 0000000000..781cf602e3 --- /dev/null +++ b/apps/opencs/view/tools/subviews.cpp @@ -0,0 +1,12 @@ + +#include "subviews.hpp" + +#include "../doc/subviewfactoryimp.hpp" + +#include "reportsubview.hpp" + +void CSVTools::addSubViewFactories (CSVDoc::SubViewFactoryManager& manager) +{ + manager.add (CSMWorld::UniversalId::Type_VerificationResults, + new CSVDoc::SubViewFactory); +} \ No newline at end of file diff --git a/apps/opencs/view/tools/subviews.hpp b/apps/opencs/view/tools/subviews.hpp new file mode 100644 index 0000000000..1bac322282 --- /dev/null +++ b/apps/opencs/view/tools/subviews.hpp @@ -0,0 +1,14 @@ +#ifndef CSV_TOOLS_SUBVIEWS_H +#define CSV_TOOLS_SUBVIEWS_H + +namespace CSVDoc +{ + class SubViewFactoryManager; +} + +namespace CSVTools +{ + void addSubViewFactories (CSVDoc::SubViewFactoryManager& manager); +} + +#endif