36 #include <QMessageBox>
37 #include <QDialogButtonBox>
38 #include <QMetaMethod>
39 #include "boost/bind.hpp"
40 #include "libQtSignalAdapters/Qt2Func.h"
41 #include "libQtSignalAdapters/ConnectionFactories.h"
42 #include <QVBoxLayout>
51 mSecondaryLayoutActionGroup = NULL;
52 mLayoutActionGroup = NULL;
53 this->createActions();
54 connect(
viewService().
get(), SIGNAL(activeLayoutChanged()),
this, SLOT(layoutChangedSlot()));
62 void LayoutInteractor::createActions()
64 mNewLayoutAction =
new QAction(tr(
"New Layout"),
this);
65 mNewLayoutAction->setToolTip(
"Create a new Custom Layout");
66 connect(mNewLayoutAction, SIGNAL(triggered()),
this, SLOT(newCustomLayoutSlot()));
67 mEditLayoutAction =
new QAction(tr(
"Edit Layout"),
this);
68 mEditLayoutAction->setToolTip(
"Edit the current Custom Layout");
69 connect(mEditLayoutAction, SIGNAL(triggered()),
this, SLOT(editCustomLayoutSlot()));
70 mDeleteLayoutAction =
new QAction(tr(
"Delete Layout"),
this);
71 mDeleteLayoutAction->setToolTip(
"Delete the current Custom Layout");
72 connect(mDeleteLayoutAction, SIGNAL(triggered()),
this, SLOT(deleteCustomLayoutSlot()));
79 mMenu->addAction(mNewLayoutAction);
80 mMenu->addAction(mEditLayoutAction);
81 mMenu->addAction(mDeleteLayoutAction);
83 mSecondaryLayoutMenu =
new QMenu(
"Secondary Layout", mMenu);
84 mMenu->addMenu(mSecondaryLayoutMenu);
86 mMenu->addSeparator();
88 this->layoutChangedSlot();
92 void LayoutInteractor::newCustomLayoutSlot()
94 LayoutData data = this->executeLayoutEditorDialog(
"New Custom Layout",
true);
95 if (data.
getUid().isEmpty())
97 this->getRepo()->insert(data);
101 void LayoutInteractor::editCustomLayoutSlot()
103 LayoutData data = this->executeLayoutEditorDialog(
"Edit Current Layout",
false);
104 if (data.getUid().isEmpty())
106 this->getRepo()->insert(data);
109 void LayoutInteractor::deleteCustomLayoutSlot()
111 if (QMessageBox::question(NULL,
"Delete current layout",
"Do you really want to delete the current layout?",
112 QMessageBox::Cancel | QMessageBox::Ok) != QMessageBox::Ok)
114 this->getRepo()->erase(
viewService()->getActiveLayout());
115 viewService()->setActiveLayout(this->getRepo()->getAvailable().front());
120 void LayoutInteractor::layoutChangedSlot()
124 if (!this->getRepo())
128 this->deepDeleteActionGroup(mLayoutActionGroup);
129 mLayoutActionGroup = this->createLayoutActionGroup(0);
130 mMenu->addActions(mLayoutActionGroup->actions());
132 this->deepDeleteActionGroup(mSecondaryLayoutActionGroup);
133 mSecondaryLayoutActionGroup = this->createLayoutActionGroup(1);
134 mSecondaryLayoutMenu->addActions(mSecondaryLayoutActionGroup->actions());
136 bool editable = this->getRepo()->isCustom(
viewService()->getActiveLayout());
137 mEditLayoutAction->setEnabled(editable);
138 mDeleteLayoutAction->setEnabled(editable);
141 void LayoutInteractor::deepDeleteActionGroup(QActionGroup* actionGroup)
146 QList<QAction*> actionList = actionGroup->actions();
147 for (
int i = 0; i < actionList.size(); i++)
148 delete actionList.at(i);
157 LayoutData LayoutInteractor::executeLayoutEditorDialog(QString title,
bool createNew)
159 boost::shared_ptr<QDialog> dialog(
new QDialog(NULL, Qt::Dialog));
160 dialog->setWindowTitle(title);
161 QVBoxLayout* layout =
new QVBoxLayout(dialog.get());
162 layout->setMargin(0);
164 LayoutEditorWidget* editor =
new LayoutEditorWidget(dialog.get());
166 LayoutData data = this->getRepo()->get(
viewService()->getActiveLayout());
170 data.resetUid(this->getRepo()->generateUid());
172 editor->setLayoutData(data);
173 layout->addWidget(editor);
175 QDialogButtonBox* buttonBox =
new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
176 connect(buttonBox, SIGNAL(accepted()), dialog.get(), SLOT(accept()));
177 connect(buttonBox, SIGNAL(rejected()), dialog.get(), SLOT(reject()));
178 layout->addWidget(buttonBox);
183 return editor->getLayoutData();
186 QActionGroup* LayoutInteractor::createLayoutActionGroup(
int widgetIndex)
188 QActionGroup* retval =
new QActionGroup(
this);
189 retval->setExclusive(
true);
190 if (!this->getRepo())
195 std::vector<QString> layouts = this->getRepo()->getAvailable();
196 int defaultLayouts = 0;
197 for (
unsigned i = 0; i < layouts.size(); ++i)
199 if (!this->getRepo()->isCustom(layouts[i]))
201 this->addLayoutAction(layouts[i], retval, widgetIndex);
207 QAction* sep =
new QAction(retval);
208 sep->setSeparator(
true);
211 if (defaultLayouts != layouts.size())
213 QAction* action =
new QAction(
"Custom", retval);
214 action->setEnabled(
false);
218 for (
unsigned i = 0; i < layouts.size(); ++i)
220 if (this->getRepo()->isCustom(layouts[i]))
221 this->addLayoutAction(layouts[i], retval, widgetIndex);
225 QString type =
viewService()->getActiveLayout(widgetIndex);
226 QList<QAction*> actions = retval->actions();
227 for (
int i = 0; i < actions.size(); ++i)
229 if (actions[i]->data().toString() == type)
230 actions[i]->setChecked(
true);
238 QAction* LayoutInteractor::addLayoutAction(QString layout, QActionGroup* group,
int widgetIndex)
240 LayoutData data = this->getRepo()->get(layout);
243 QAction* sep =
new QAction(group);
244 sep->setSeparator(
true);
246 QAction* action =
new QAction(data.getName(), group);
247 action->setEnabled(!data.isEmpty());
248 action->setCheckable(!data.isEmpty());
249 action->setData(QVariant(layout));
252 QtSignalAdapters::connect0<void()>(
255 boost::bind(&LayoutInteractor::setActiveLayout,
this, layout, widgetIndex));
260 void LayoutInteractor::setActiveLayout(QString layout,
int widgetIndex)
263 viewService()->setActiveLayout(layout, widgetIndex);
boost::shared_ptr< class LayoutRepository > LayoutRepositoryPtr
void connectToMenu(QMenu *menu)
cxLogicManager_EXPORT ViewServicePtr viewService()
LayoutInteractor(QObject *parent=NULL)