make editor plugin mechanism work plugin
authorKonrad Rosenbaum <Konrad.Rosenbaum@peergroup.com>
Mon, 28 Mar 2016 18:38:35 +0000 (20:38 +0200)
committerKonrad Rosenbaum <Konrad.Rosenbaum@peergroup.com>
Mon, 28 Mar 2016 18:38:35 +0000 (20:38 +0200)
src/dialogs/editreg.cpp
src/misc/editor-plugin.cpp
src/misc/editor-plugin.h
src/mwin/eventstab.cpp

index fe680dd..dd91f23 100644 (file)
 
 void registerEditors()
 {
-       MEditorPlugin::registerEditor<MEventEditor>(MEditorPlugin::EditorType::EventEdit,MEditorPlugin::GuiMode::Expert);
-       MEditorPlugin::registerEditor<MEventEditor>(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");
 }
index 2c2187a..d2a6d77 100644 (file)
@@ -22,21 +22,20 @@ MEditorPlugin::~MEditorPlugin()
 
 static QMap<QPair<MEditorPlugin::EditorType,MEditorPlugin::GuiMode>,QList<MEditorPlugin::EditorInfo>>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)
 {
-       QPair<EditorType,GuiMode>key(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;
        QPair<EditorType,GuiMode>key(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();i<meta->classInfoCount();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<MEditorPlugin::EditorInfo> 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)
 {
        QPair<EditorType,GuiMode>key(et,gm);
@@ -65,17 +68,17 @@ QList< MEditorPlugin::EditorInfo > MEditorPlugin::getEditorClassInfo(MEditorPlug
        return QList<EditorInfo>();
 }
 
-void MEditorPlugin::setEditorDefault(MEditorPlugin::EditorType et, MEditorPlugin::GuiMode gm, const QMetaObject*meta)
+void MEditorPlugin::setEditorDefault(MEditorPlugin::EditorType et, MEditorPlugin::GuiMode gm, QString id)
 {
        QPair<EditorType,GuiMode>key(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;
 }
index 914fef3..ea60d4b 100644 (file)
 
 #include "../smoke-export.h"
 
-#include <QtPlugin>
-#include <QStringList>
+#include <QCoreApplication>
 #include <QDebug>
 #include <QDialog>
 #include <QList>
-#include <QMetaType>
+#include <QMessageBox>
 #include <QMetaObject>
+#include <QMetaType>
+#include <QStringList>
+#include <QtPlugin>
+
+#include <functional>
 
 ///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<bool(QWidget*,ViewMode,int)> 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<typename T>
-                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<typename T>
-               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<EditorInfo> getEditorClassInfo(EditorType,GuiMode);
                
-               ///called whenever a specific editor is needed
-               template<typename ...Args>
-               static QWidget* createEditor(EditorType et,GuiMode gm,Args...args)
+               static EventEditor getEventEditor(GuiMode gm){return getEditor<EventEditor>(EditorType::EventEdit,gm);}
+               static EventEditor getEventViewer(GuiMode gm){return getEditor<EventEditor>(EditorType::EventView,gm);}
+               static EventEditor getNewEvent(GuiMode gm){return getEditor<EventEditor>(EditorType::EventNew,gm);}
+       protected:
+               static void registerEditor(EditorType type,GuiMode mode,void*,QString id,QString d=QString());
+               
+               template<typename Ret,typename ...Args>
+               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 <typename Editor>
+               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*>(editor));
+//                     else 
+//                             return (Editor)(&showWarning);
                }
+               
+               static void*getEditorLL(EditorType,GuiMode);
 };
 
 #define MEditorPlugin_IID "de.silmor.MagicSmoke.EditorPlugin/1.0"
index 912c63d..d7365b4 100644 (file)
@@ -306,13 +306,9 @@ void MEventsTab::eventOrderTicketUrl(const QUrl &url)
 
 void MEventsTab::newEvent()
 {
-//     MEventEditor ed(this);
-       QDialog *ed=qobject_cast<QDialog*>(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()