15 #include <QMessageBox>
16 #include <QDialogButtonBox>
17 #include <QMetaMethod>
18 #include "boost/bind.hpp"
19 #include "libQtSignalAdapters/Qt2Func.h"
20 #include "libQtSignalAdapters/ConnectionFactories.h"
21 #include <QVBoxLayout>
30 mViewService(viewService)
32 mSecondaryLayoutActionGroup = NULL;
33 mLayoutActionGroup = NULL;
34 this->createActions();
35 connect(viewService.get(), SIGNAL(activeLayoutChanged()),
this, SLOT(layoutChangedSlot()));
40 return mViewService->getLayoutRepository();
43 void LayoutInteractor::createActions()
45 mNewLayoutAction =
new QAction(tr(
"New Layout"),
this);
46 mNewLayoutAction->setToolTip(
"Create a new Custom Layout");
47 connect(mNewLayoutAction, SIGNAL(triggered()),
this, SLOT(newCustomLayoutSlot()));
48 mEditLayoutAction =
new QAction(tr(
"Edit Layout"),
this);
49 mEditLayoutAction->setToolTip(
"Edit the current Custom Layout");
50 connect(mEditLayoutAction, SIGNAL(triggered()),
this, SLOT(editCustomLayoutSlot()));
51 mDeleteLayoutAction =
new QAction(tr(
"Delete Layout"),
this);
52 mDeleteLayoutAction->setToolTip(
"Delete the current Custom Layout");
53 connect(mDeleteLayoutAction, SIGNAL(triggered()),
this, SLOT(deleteCustomLayoutSlot()));
60 mMenu->addAction(mNewLayoutAction);
61 mMenu->addAction(mEditLayoutAction);
62 mMenu->addAction(mDeleteLayoutAction);
64 mSecondaryLayoutMenu =
new QMenu(
"Secondary Layout", mMenu);
65 mMenu->addMenu(mSecondaryLayoutMenu);
67 mMenu->addSeparator();
69 this->layoutChangedSlot();
73 void LayoutInteractor::newCustomLayoutSlot()
75 LayoutData data = this->executeLayoutEditorDialog(
"New Custom Layout",
true);
76 if (data.
getUid().isEmpty())
78 this->getRepo()->insert(data);
79 mViewService->setActiveLayout(data.
getUid());
82 void LayoutInteractor::editCustomLayoutSlot()
84 LayoutData data = this->executeLayoutEditorDialog(
"Edit Current Layout",
false);
85 if (data.getUid().isEmpty())
87 this->getRepo()->insert(data);
90 void LayoutInteractor::deleteCustomLayoutSlot()
92 if (QMessageBox::question(NULL,
"Delete current layout",
"Do you really want to delete the current layout?",
93 QMessageBox::Cancel | QMessageBox::Ok) != QMessageBox::Ok)
95 this->getRepo()->erase(mViewService->getActiveLayout());
96 mViewService->setActiveLayout(this->getRepo()->getAvailable().front());
101 void LayoutInteractor::layoutChangedSlot()
105 if (!this->getRepo())
109 this->deepDeleteActionGroup(mLayoutActionGroup);
110 mLayoutActionGroup = this->createLayoutActionGroup(0);
111 mMenu->addActions(mLayoutActionGroup->actions());
113 this->deepDeleteActionGroup(mSecondaryLayoutActionGroup);
114 mSecondaryLayoutActionGroup = this->createLayoutActionGroup(1);
115 mSecondaryLayoutMenu->addActions(mSecondaryLayoutActionGroup->actions());
117 bool editable = this->getRepo()->isCustom(mViewService->getActiveLayout());
118 mEditLayoutAction->setEnabled(editable);
119 mDeleteLayoutAction->setEnabled(editable);
122 void LayoutInteractor::deepDeleteActionGroup(QActionGroup* actionGroup)
127 QList<QAction*> actionList = actionGroup->actions();
128 for (
int i = 0; i < actionList.size(); i++)
129 delete actionList.at(i);
138 LayoutData LayoutInteractor::executeLayoutEditorDialog(QString title,
bool createNew)
140 boost::shared_ptr<QDialog> dialog(
new QDialog(NULL, Qt::Dialog));
141 dialog->setWindowTitle(title);
142 QVBoxLayout* layout =
new QVBoxLayout(dialog.get());
143 layout->setMargin(0);
145 LayoutEditorWidget* editor =
new LayoutEditorWidget(mViewService, dialog.get());
147 LayoutData data = this->getRepo()->get(mViewService->getActiveLayout());
151 data.resetUid(this->getRepo()->generateUid());
153 editor->setLayoutData(data);
154 layout->addWidget(editor);
156 QDialogButtonBox* buttonBox =
new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
157 connect(buttonBox, SIGNAL(accepted()), dialog.get(), SLOT(accept()));
158 connect(buttonBox, SIGNAL(rejected()), dialog.get(), SLOT(reject()));
159 layout->addWidget(buttonBox);
164 return editor->getLayoutData();
167 QActionGroup* LayoutInteractor::createLayoutActionGroup(
int widgetIndex)
169 QActionGroup* retval =
new QActionGroup(
this);
170 retval->setExclusive(
true);
171 if (!this->getRepo())
176 std::vector<QString> layouts = this->getRepo()->getAvailable();
177 int defaultLayouts = 0;
178 for (
unsigned i = 0; i < layouts.size(); ++i)
180 if (!this->getRepo()->isCustom(layouts[i]))
182 this->addLayoutAction(layouts[i], retval, widgetIndex);
188 QAction* sep =
new QAction(retval);
189 sep->setSeparator(
true);
192 if (defaultLayouts != layouts.size())
194 QAction* action =
new QAction(
"Custom", retval);
195 action->setEnabled(
false);
199 for (
unsigned i = 0; i < layouts.size(); ++i)
201 if (this->getRepo()->isCustom(layouts[i]))
202 this->addLayoutAction(layouts[i], retval, widgetIndex);
206 QString type = mViewService->getActiveLayout(widgetIndex);
207 QList<QAction*> actions = retval->actions();
208 for (
int i = 0; i < actions.size(); ++i)
210 if (actions[i]->data().toString() == type)
211 actions[i]->setChecked(
true);
219 QAction* LayoutInteractor::addLayoutAction(QString layout, QActionGroup* group,
int widgetIndex)
221 LayoutData data = this->getRepo()->get(layout);
224 QAction* sep =
new QAction(group);
225 sep->setSeparator(
true);
227 QAction* action =
new QAction(data.getName(), group);
228 action->setEnabled(!data.isEmpty());
229 action->setCheckable(!data.isEmpty());
230 action->setData(QVariant(layout));
233 QtSignalAdapters::connect0<void()>(
236 boost::bind(&LayoutInteractor::setActiveLayout,
this, layout, widgetIndex));
241 void LayoutInteractor::setActiveLayout(QString layout,
int widgetIndex)
244 mViewService->setActiveLayout(layout, widgetIndex);