From b9b429dda001d692f7c13fc816bdf5646697afb5 Mon Sep 17 00:00:00 2001 From: Konrad Rosenbaum Date: Mon, 28 Mar 2016 20:38:35 +0200 Subject: [PATCH] make editor plugin mechanism work --- src/dialogs/editreg.cpp | 25 +++++++++++++- src/misc/editor-plugin.cpp | 49 +++++++++++++++------------- src/misc/editor-plugin.h | 76 +++++++++++++++++++++++++------------------- src/mwin/eventstab.cpp | 22 +++++------- 4 files changed, 101 insertions(+), 71 deletions(-) diff --git a/src/dialogs/editreg.cpp b/src/dialogs/editreg.cpp index fe680dd..dd91f23 100644 --- a/src/dialogs/editreg.cpp +++ b/src/dialogs/editreg.cpp @@ -18,6 +18,27 @@ void registerEditors() { - MEditorPlugin::registerEditor(MEditorPlugin::EditorType::EventEdit,MEditorPlugin::GuiMode::Expert); - MEditorPlugin::registerEditor(MEditorPlugin::EditorType::EventView,MEditorPlugin::GuiMode::Expert); + MEditorPlugin::EventEditor eventedit=[](QWidget*parent,MEditorPlugin::ViewMode vm,int eid)->bool{ + MEventEditor*ed=new MEventEditor(parent,eid); + ed->setAttribute(Qt::WA_DeleteOnClose); + if(vm==MEditorPlugin::ViewMode::Exec){ + ed->exec(); + return ed->result()==QDialog::Accepted; + }else + ed->show(); + return false; + }; + MEditorPlugin::EventEditor eventnew=[](QWidget*parent,MEditorPlugin::ViewMode vm,int eid)->bool{ + MEventEditor*ed=new MEventEditor(parent,MEventEditor::OpenMode::Create,eid); + ed->setAttribute(Qt::WA_DeleteOnClose); + if(vm==MEditorPlugin::ViewMode::Exec){ + ed->exec(); + return ed->result()==QDialog::Accepted; + }else + ed->show(); + return false; + }; + MEditorPlugin::registerEditor(MEditorPlugin::EditorType::EventEdit,MEditorPlugin::GuiMode::Expert,eventedit,"EventEdit-default"); + MEditorPlugin::registerEditor(MEditorPlugin::EditorType::EventView,MEditorPlugin::GuiMode::Expert,eventedit,"EventEdit-default"); + MEditorPlugin::registerEditor(MEditorPlugin::EditorType::EventNew, MEditorPlugin::GuiMode::Expert,eventnew,"EventEdit-new"); } diff --git a/src/misc/editor-plugin.cpp b/src/misc/editor-plugin.cpp index 2c2187a..d2a6d77 100644 --- a/src/misc/editor-plugin.cpp +++ b/src/misc/editor-plugin.cpp @@ -22,21 +22,20 @@ MEditorPlugin::~MEditorPlugin() static QMap,QList>editormap; -const QMetaObject* MEditorPlugin::getEditorMetaObject(MEditorPlugin::EditorType et, MEditorPlugin::GuiMode gm) +void MEditorPlugin::registerEditor(MEditorPlugin::EditorType type, MEditorPlugin::GuiMode mode, MEditorPlugin::EventEditor editor, QString id, QString d) { - QPairkey(et,gm); - if(editormap.contains(key)){ - //find the default - for(auto info:editormap[key]) - if(info.isDefault)return info.metaObject; - //no default: use first - if(editormap[key].size()>0)return editormap[key][0].metaObject; + switch(type){ + case MEditorPlugin::EditorType::EventEdit: + case MEditorPlugin::EditorType::EventNew: + case MEditorPlugin::EditorType::EventView: + registerEditor(type,mode,new EventEditor(editor),id,d); + break; + default: + break; } - //fallback: none - return nullptr; } -void MEditorPlugin::registerEditor(MEditorPlugin::EditorType type, MEditorPlugin::GuiMode mode, const QMetaObject*meta,QString description) +void MEditorPlugin::registerEditor(MEditorPlugin::EditorType type, MEditorPlugin::GuiMode mode, void*editor, QString id, QString d) { if(mode==GuiMode::None || type==EditorType::NoEditor)return; QPairkey(type,mode); @@ -45,18 +44,22 @@ void MEditorPlugin::registerEditor(MEditorPlugin::EditorType type, MEditorPlugin EditorInfo info; info.editorType=type; info.guiMode=mode; - info.metaObject=meta; - if(description.isEmpty()){ - if(meta){ - for(int i=meta->classInfoOffset();iclassInfoCount();i++) - if(meta->classInfo(i).name()==QString("description")) - info.description=meta->classInfo(i).value(); - } - }else - info.description=description; + info.constructor=editor; + info.description=d; + info.id=id; editormap[key].prepend(info); } +void* MEditorPlugin::getEditorLL(MEditorPlugin::EditorType et, MEditorPlugin::GuiMode gm) +{ + QList res=getEditorClassInfo(et,gm); + if(res.isEmpty())return nullptr; + for(auto info:res) + if(info.isDefault) + return info.constructor; + return res[0].constructor; +} + QList< MEditorPlugin::EditorInfo > MEditorPlugin::getEditorClassInfo(MEditorPlugin::EditorType et, MEditorPlugin::GuiMode gm) { QPairkey(et,gm); @@ -65,17 +68,17 @@ QList< MEditorPlugin::EditorInfo > MEditorPlugin::getEditorClassInfo(MEditorPlug return QList(); } -void MEditorPlugin::setEditorDefault(MEditorPlugin::EditorType et, MEditorPlugin::GuiMode gm, const QMetaObject*meta) +void MEditorPlugin::setEditorDefault(MEditorPlugin::EditorType et, MEditorPlugin::GuiMode gm, QString id) { QPairkey(et,gm); if(!editormap.contains(key))return; bool found=false; for(auto info:editormap[key]) - if(info.metaObject==meta){ + if(info.id==id){ found=true; break; } if(!found)return; for(auto info:editormap[key]) - info.isDefault=info.metaObject==meta; + info.isDefault=info.id==id; } diff --git a/src/misc/editor-plugin.h b/src/misc/editor-plugin.h index 914fef3..ea60d4b 100644 --- a/src/misc/editor-plugin.h +++ b/src/misc/editor-plugin.h @@ -15,13 +15,17 @@ #include "../smoke-export.h" -#include -#include +#include #include #include #include -#include +#include #include +#include +#include +#include + +#include ///base class of editor plugins class MAGICSMOKE_EXPORT MEditorPlugin @@ -33,6 +37,7 @@ class MAGICSMOKE_EXPORT MEditorPlugin NoEditor, EventView, EventEdit, + EventNew, OrderView, OrderEdit, }; @@ -41,54 +46,59 @@ class MAGICSMOKE_EXPORT MEditorPlugin None=0, Expert=1, Wizard=2, + Both=3 + }; + + enum class ViewMode{ + Show, + Exec }; struct EditorInfo{ EditorType editorType=EditorType::NoEditor; GuiMode guiMode=GuiMode::None; bool isDefault=false; - const QMetaObject*metaObject=nullptr; + QString id; QString description; + private: + friend class MEditorPlugin; + void*constructor; }; + + typedef std::function EventEditor; ///called by plugins to register a new editor -// static void registerEditor(EditorType type,GuiMode mode,const char*className,QString description=QString()) -// {registerEditor(type,mode,QMetaType::type(className),description);} - ///called by plugins to register a new editor - template - static void registerEditor(EditorType type,GuiMode mode,QString d=QString()) - { - registerEditor(type,mode,&T::staticMetaObject,d); - } - ///called by plugins to register a new editor - static void registerEditor(EditorType type,GuiMode mode,const QMetaObject*,QString d=QString()); + static void registerEditor(EditorType type,GuiMode mode,EventEditor,QString id,QString d=QString()); ///called by configuration dialog to set a specific editor as default of its class - template - static void setEditorDefault(EditorType et,GuiMode gm) - { - setEditorDefault(et,gm,&T::staticMetaObject); - } - ///called by configuration dialog to set a specific editor as default of its class - static void setEditorDefault(EditorType,GuiMode,const QMetaObject*); - - ///Returns the configured ID of the Editor. - static const QMetaObject* getEditorMetaObject(EditorType,GuiMode); + static void setEditorDefault(EditorType,GuiMode,QString); ///Returns all editors registered to a specific static QList getEditorClassInfo(EditorType,GuiMode); - ///called whenever a specific editor is needed - template - static QWidget* createEditor(EditorType et,GuiMode gm,Args...args) + static EventEditor getEventEditor(GuiMode gm){return getEditor(EditorType::EventEdit,gm);} + static EventEditor getEventViewer(GuiMode gm){return getEditor(EditorType::EventView,gm);} + static EventEditor getNewEvent(GuiMode gm){return getEditor(EditorType::EventNew,gm);} + protected: + static void registerEditor(EditorType type,GuiMode mode,void*,QString id,QString d=QString()); + + template + static Ret showWarning(QWidget*parent,Args...args){ + QMessageBox::warning(parent,QCoreApplication::translate("MEditorPlugin","Warning"), QCoreApplication::translate("MEditorPlugin","Unable to open this editor: cannot find it.")); + return Ret(); + } + + template + static Editor getEditor(EditorType et,GuiMode gm) { - const QMetaObject*mobj=getEditorMetaObject(et,gm); - qDebug()<<"meta"<<(int)mobj; - if(!mobj)return nullptr; - QObject*obj=mobj->newInstance(Q_ARG(Args,args)...); - qDebug()<<"obj"<<(int)obj; - return qobject_cast< QWidget* >(obj); + void * editor=getEditorLL(et,gm); +// if(editor) + return *(reinterpret_cast(editor)); +// else +// return (Editor)(&showWarning); } + + static void*getEditorLL(EditorType,GuiMode); }; #define MEditorPlugin_IID "de.silmor.MagicSmoke.EditorPlugin/1.0" diff --git a/src/mwin/eventstab.cpp b/src/mwin/eventstab.cpp index 912c63d..d7365b4 100644 --- a/src/mwin/eventstab.cpp +++ b/src/mwin/eventstab.cpp @@ -306,13 +306,9 @@ void MEventsTab::eventOrderTicketUrl(const QUrl &url) void MEventsTab::newEvent() { -// MEventEditor ed(this); - QDialog *ed=qobject_cast(MEditorPlugin::createEditor(MEditorPlugin::EditorType::EventEdit,MEditorPlugin::GuiMode::Expert,this)); - if(ed==nullptr)qDebug()<<"OUCH"; - ed->exec(); - ed->deleteLater(); -// ed.exec(); - updateEvents(); + auto ed=MEditorPlugin::getNewEvent(MEditorPlugin::GuiMode::Expert); + if(ed(this,MEditorPlugin::ViewMode::Exec,-1)) + updateEvents(); } void MEventsTab::cloneEvent() @@ -323,9 +319,9 @@ void MEventsTab::cloneEvent() QModelIndex idx=eventmodel->index(ilst[0].row(),0); id=eventmodel->data(idx,IdRole).toInt(); if(id<0)return; - MEventEditor ed(this,MEventEditor::OpenMode::Create,id); - ed.exec(); - updateEvents(); + auto ed=MEditorPlugin::getNewEvent(MEditorPlugin::GuiMode::Expert); + if(ed(this,MEditorPlugin::ViewMode::Exec,id)) + updateEvents(); } void MEventsTab::editEvent() @@ -336,9 +332,9 @@ void MEventsTab::editEvent() QModelIndex idx=eventmodel->index(ilst[0].row(),0); id=eventmodel->data(idx,IdRole).toInt(); if(id<0)return; - MEventEditor ed(this,id); - ed.exec(); - updateEvents(); + auto ed=MEditorPlugin::getEventEditor(MEditorPlugin::GuiMode::Expert); + if(ed(this,MEditorPlugin::ViewMode::Exec,id)) + updateEvents(); } void MEventsTab::eventSummary() -- 1.7.2.5