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 mViewService(viewService)
53 mSecondaryLayoutActionGroup = NULL;
54 mLayoutActionGroup = NULL;
55 this->createActions();
56 connect(viewService.get(), SIGNAL(activeLayoutChanged()),
this, SLOT(layoutChangedSlot()));
61 return mViewService->getLayoutRepository();
64 void LayoutInteractor::createActions()
66 mNewLayoutAction =
new QAction(tr(
"New Layout"),
this);
67 mNewLayoutAction->setToolTip(
"Create a new Custom Layout");
68 connect(mNewLayoutAction, SIGNAL(triggered()),
this, SLOT(newCustomLayoutSlot()));
69 mEditLayoutAction =
new QAction(tr(
"Edit Layout"),
this);
70 mEditLayoutAction->setToolTip(
"Edit the current Custom Layout");
71 connect(mEditLayoutAction, SIGNAL(triggered()),
this, SLOT(editCustomLayoutSlot()));
72 mDeleteLayoutAction =
new QAction(tr(
"Delete Layout"),
this);
73 mDeleteLayoutAction->setToolTip(
"Delete the current Custom Layout");
74 connect(mDeleteLayoutAction, SIGNAL(triggered()),
this, SLOT(deleteCustomLayoutSlot()));
81 mMenu->addAction(mNewLayoutAction);
82 mMenu->addAction(mEditLayoutAction);
83 mMenu->addAction(mDeleteLayoutAction);
85 mSecondaryLayoutMenu =
new QMenu(
"Secondary Layout", mMenu);
86 mMenu->addMenu(mSecondaryLayoutMenu);
88 mMenu->addSeparator();
90 this->layoutChangedSlot();
94 void LayoutInteractor::newCustomLayoutSlot()
96 LayoutData data = this->executeLayoutEditorDialog(
"New Custom Layout",
true);
97 if (data.
getUid().isEmpty())
99 this->getRepo()->insert(data);
100 mViewService->setActiveLayout(data.
getUid());
103 void LayoutInteractor::editCustomLayoutSlot()
105 LayoutData data = this->executeLayoutEditorDialog(
"Edit Current Layout",
false);
106 if (data.
getUid().isEmpty())
108 this->getRepo()->insert(data);
111 void LayoutInteractor::deleteCustomLayoutSlot()
113 if (QMessageBox::question(NULL,
"Delete current layout",
"Do you really want to delete the current layout?",
114 QMessageBox::Cancel | QMessageBox::Ok) != QMessageBox::Ok)
116 this->getRepo()->erase(mViewService->getActiveLayout());
117 mViewService->setActiveLayout(this->getRepo()->getAvailable().front());
122 void LayoutInteractor::layoutChangedSlot()
126 if (!this->getRepo())
130 this->deepDeleteActionGroup(mLayoutActionGroup);
131 mLayoutActionGroup = this->createLayoutActionGroup(0);
132 mMenu->addActions(mLayoutActionGroup->actions());
134 this->deepDeleteActionGroup(mSecondaryLayoutActionGroup);
135 mSecondaryLayoutActionGroup = this->createLayoutActionGroup(1);
136 mSecondaryLayoutMenu->addActions(mSecondaryLayoutActionGroup->actions());
138 bool editable = this->getRepo()->isCustom(mViewService->getActiveLayout());
139 mEditLayoutAction->setEnabled(editable);
140 mDeleteLayoutAction->setEnabled(editable);
143 void LayoutInteractor::deepDeleteActionGroup(QActionGroup* actionGroup)
148 QList<QAction*> actionList = actionGroup->actions();
149 for (
int i = 0; i < actionList.size(); i++)
150 delete actionList.at(i);
159 LayoutData LayoutInteractor::executeLayoutEditorDialog(QString title,
bool createNew)
161 boost::shared_ptr<QDialog> dialog(
new QDialog(NULL, Qt::Dialog));
162 dialog->setWindowTitle(title);
163 QVBoxLayout* layout =
new QVBoxLayout(dialog.get());
164 layout->setMargin(0);
168 LayoutData data = this->getRepo()->get(mViewService->getActiveLayout());
172 data.resetUid(this->getRepo()->generateUid());
175 layout->addWidget(editor);
177 QDialogButtonBox* buttonBox =
new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
178 connect(buttonBox, SIGNAL(accepted()), dialog.get(), SLOT(accept()));
179 connect(buttonBox, SIGNAL(rejected()), dialog.get(), SLOT(reject()));
180 layout->addWidget(buttonBox);
188 QActionGroup* LayoutInteractor::createLayoutActionGroup(
int widgetIndex)
190 QActionGroup* retval =
new QActionGroup(
this);
191 retval->setExclusive(
true);
192 if (!this->getRepo())
197 std::vector<QString> layouts = this->getRepo()->getAvailable();
198 int defaultLayouts = 0;
199 for (
unsigned i = 0; i < layouts.size(); ++i)
201 if (!this->getRepo()->isCustom(layouts[i]))
203 this->addLayoutAction(layouts[i], retval, widgetIndex);
209 QAction* sep =
new QAction(retval);
210 sep->setSeparator(
true);
213 if (defaultLayouts != layouts.size())
215 QAction* action =
new QAction(
"Custom", retval);
216 action->setEnabled(
false);
220 for (
unsigned i = 0; i < layouts.size(); ++i)
222 if (this->getRepo()->isCustom(layouts[i]))
223 this->addLayoutAction(layouts[i], retval, widgetIndex);
227 QString type = mViewService->getActiveLayout(widgetIndex);
228 QList<QAction*> actions = retval->actions();
229 for (
int i = 0; i < actions.size(); ++i)
231 if (actions[i]->data().toString() == type)
232 actions[i]->setChecked(
true);
240 QAction* LayoutInteractor::addLayoutAction(QString layout, QActionGroup* group,
int widgetIndex)
242 LayoutData data = this->getRepo()->get(layout);
245 QAction* sep =
new QAction(group);
246 sep->setSeparator(
true);
248 QAction* action =
new QAction(data.
getName(), group);
249 action->setEnabled(!data.
isEmpty());
250 action->setCheckable(!data.
isEmpty());
251 action->setData(QVariant(layout));
254 QtSignalAdapters::connect0<void()>(
257 boost::bind(&LayoutInteractor::setActiveLayout,
this, layout, widgetIndex));
262 void LayoutInteractor::setActiveLayout(QString layout,
int widgetIndex)
265 mViewService->setActiveLayout(layout, widgetIndex);
boost::shared_ptr< class LayoutRepository > LayoutRepositoryPtr
void connectToMenu(QMenu *menu)
LayoutInteractor(ViewServicePtr viewService, QObject *parent=NULL)
Namespace for all CustusX production code.