ToolBarManager.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <QApplication>
00027 # include <QAction>
00028 # include <QToolBar>
00029 # include <QToolButton>
00030 #endif
00031
00032 #include "ToolBarManager.h"
00033 #include "MainWindow.h"
00034 #include "Application.h"
00035 #include "Command.h"
00036
00037 using namespace Gui;
00038
00039 ToolBarItem::ToolBarItem()
00040 {
00041 }
00042
00043 ToolBarItem::ToolBarItem(ToolBarItem* item)
00044 {
00045 if ( item )
00046 item->appendItem(this);
00047 }
00048
00049 ToolBarItem::~ToolBarItem()
00050 {
00051 clear();
00052 }
00053
00054 void ToolBarItem::setCommand(const std::string& name)
00055 {
00056 _name = name;
00057 }
00058
00059 std::string ToolBarItem::command() const
00060 {
00061 return _name;
00062 }
00063
00064 bool ToolBarItem::hasItems() const
00065 {
00066 return _items.count() > 0;
00067 }
00068
00069 ToolBarItem* ToolBarItem::findItem(const std::string& name)
00070 {
00071 if ( _name == name ) {
00072 return this;
00073 } else {
00074 for ( QList<ToolBarItem*>::ConstIterator it = _items.begin(); it != _items.end(); ++it ) {
00075 if ( (*it)->_name == name ) {
00076 return *it;
00077 }
00078 }
00079 }
00080
00081 return 0;
00082 }
00083
00084 ToolBarItem* ToolBarItem::copy() const
00085 {
00086 ToolBarItem* root = new ToolBarItem;
00087 root->setCommand( command() );
00088
00089 QList<ToolBarItem*> items = getItems();
00090 for ( QList<ToolBarItem*>::ConstIterator it = items.begin(); it != items.end(); ++it ) {
00091 root->appendItem( (*it)->copy() );
00092 }
00093
00094 return root;
00095 }
00096
00097 uint ToolBarItem::count() const
00098 {
00099 return _items.count();
00100 }
00101
00102 void ToolBarItem::appendItem(ToolBarItem* item)
00103 {
00104 _items.push_back( item );
00105 }
00106
00107 bool ToolBarItem::insertItem( ToolBarItem* before, ToolBarItem* item)
00108 {
00109 int pos = _items.indexOf(before);
00110 if (pos != -1) {
00111 _items.insert(pos, item);
00112 return true;
00113 } else
00114 return false;
00115 }
00116
00117 void ToolBarItem::removeItem(ToolBarItem* item)
00118 {
00119 int pos = _items.indexOf(item);
00120 if (pos != -1)
00121 _items.removeAt(pos);
00122 }
00123
00124 void ToolBarItem::clear()
00125 {
00126 for ( QList<ToolBarItem*>::Iterator it = _items.begin(); it != _items.end(); ++it ) {
00127 delete *it;
00128 }
00129
00130 _items.clear();
00131 }
00132
00133 ToolBarItem& ToolBarItem::operator << (ToolBarItem* item)
00134 {
00135 appendItem(item);
00136 return *this;
00137 }
00138
00139 ToolBarItem& ToolBarItem::operator << (const std::string& command)
00140 {
00141 ToolBarItem* item = new ToolBarItem(this);
00142 item->setCommand(command);
00143 return *this;
00144 }
00145
00146 QList<ToolBarItem*> ToolBarItem::getItems() const
00147 {
00148 return _items;
00149 }
00150
00151
00152
00153 ToolBarManager* ToolBarManager::_instance=0;
00154
00155 ToolBarManager* ToolBarManager::getInstance()
00156 {
00157 if ( !_instance )
00158 _instance = new ToolBarManager;
00159 return _instance;
00160 }
00161
00162 void ToolBarManager::destruct()
00163 {
00164 delete _instance;
00165 _instance = 0;
00166 }
00167
00168 ToolBarManager::ToolBarManager()
00169 {
00170 }
00171
00172 ToolBarManager::~ToolBarManager()
00173 {
00174 }
00175
00176 void ToolBarManager::setup(ToolBarItem* toolBarItems)
00177 {
00178 if (!toolBarItems)
00179 return;
00180
00181 saveState();
00182 this->toolbarNames.clear();
00183
00184 int max_width = getMainWindow()->width();
00185 int top_width = 0;
00186
00187 ParameterGrp::handle hPref = App::GetApplication().GetUserParameter().GetGroup("BaseApp")
00188 ->GetGroup("MainWindow")->GetGroup("Toolbars");
00189 QList<ToolBarItem*> items = toolBarItems->getItems();
00190 QList<QToolBar*> toolbars = toolBars();
00191 for (QList<ToolBarItem*>::ConstIterator it = items.begin(); it != items.end(); ++it) {
00192
00193 this->toolbarNames << QString::fromUtf8((*it)->command().c_str());
00194 QToolBar* toolbar = findToolBar(toolbars, QString::fromAscii((*it)->command().c_str()));
00195 std::string toolbarName = (*it)->command();
00196 bool visible = hPref->GetBool(toolbarName.c_str(), true);
00197 bool toolbar_added = false;
00198
00199 if (!toolbar) {
00200 toolbar = getMainWindow()->addToolBar(
00201 QApplication::translate("Workbench",
00202 toolbarName.c_str(),
00203 0, QApplication::UnicodeUTF8));
00204 toolbar->setObjectName(QString::fromAscii((*it)->command().c_str()));
00205 toolbar->setVisible(visible);
00206 toolbar_added = true;
00207 }
00208 else {
00209 toolbar->setVisible(visible);
00210 toolbar->toggleViewAction()->setVisible(true);
00211 int index = toolbars.indexOf(toolbar);
00212 toolbars.removeAt(index);
00213 }
00214
00215
00216 setup(*it, toolbar);
00217
00218
00219 if (toolbar_added) {
00220 if (top_width > 0 && getMainWindow()->toolBarBreak(toolbar))
00221 top_width = 0;
00222
00223
00224 QList<QToolButton*> btns = toolbar->findChildren<QToolButton*>();
00225 top_width += (btns.size() * toolbar->iconSize().width());
00226 if (top_width > max_width) {
00227 top_width = 0;
00228 getMainWindow()->insertToolBarBreak(toolbar);
00229 }
00230 }
00231 }
00232
00233
00234 for (QList<QToolBar*>::Iterator it = toolbars.begin(); it != toolbars.end(); ++it) {
00235
00236 QByteArray toolbarName = (*it)->objectName().toUtf8();
00237 if (!(*it)->toggleViewAction()->isVisible())
00238 continue;
00239 hPref->SetBool(toolbarName.constData(), (*it)->isVisible());
00240 (*it)->hide();
00241 (*it)->toggleViewAction()->setVisible(false);
00242 }
00243 }
00244
00245 void ToolBarManager::setup(ToolBarItem* item, QToolBar* toolbar) const
00246 {
00247 CommandManager& mgr = Application::Instance->commandManager();
00248 QList<ToolBarItem*> items = item->getItems();
00249 QList<QAction*> actions = toolbar->actions();
00250 for (QList<ToolBarItem*>::ConstIterator it = items.begin(); it != items.end(); ++it) {
00251
00252 QAction* action = findAction(actions, QString::fromAscii((*it)->command().c_str()));
00253 if (!action) {
00254 if ((*it)->command() == "Separator") {
00255 action = toolbar->addSeparator();
00256 } else {
00257
00258 if (mgr.addTo((*it)->command().c_str(), toolbar))
00259 action = toolbar->actions().last();
00260 }
00261
00262
00263 if (action) action->setData(QString::fromAscii((*it)->command().c_str()));
00264 } else {
00265
00266
00267
00268 int index = actions.indexOf(action);
00269 actions.removeAt(index);
00270 }
00271 }
00272
00273
00274 for (QList<QAction*>::Iterator it = actions.begin(); it != actions.end(); ++it) {
00275 toolbar->removeAction(*it);
00276 }
00277 }
00278
00279 void ToolBarManager::saveState() const
00280 {
00281 ParameterGrp::handle hPref = App::GetApplication().GetUserParameter().GetGroup("BaseApp")
00282 ->GetGroup("MainWindow")->GetGroup("Toolbars");
00283
00284 QList<QToolBar*> toolbars = toolBars();
00285 for (QStringList::ConstIterator it = this->toolbarNames.begin(); it != this->toolbarNames.end(); ++it) {
00286 QToolBar* toolbar = findToolBar(toolbars, *it);
00287 if (toolbar) {
00288 QByteArray toolbarName = toolbar->objectName().toUtf8();
00289 hPref->SetBool(toolbarName.constData(), toolbar->isVisible());
00290 }
00291 }
00292 }
00293
00294 void ToolBarManager::restoreState() const
00295 {
00296 ParameterGrp::handle hPref = App::GetApplication().GetUserParameter().GetGroup("BaseApp")
00297 ->GetGroup("MainWindow")->GetGroup("Toolbars");
00298
00299 QList<QToolBar*> toolbars = toolBars();
00300 for (QStringList::ConstIterator it = this->toolbarNames.begin(); it != this->toolbarNames.end(); ++it) {
00301 QToolBar* toolbar = findToolBar(toolbars, *it);
00302 if (toolbar) {
00303 QByteArray toolbarName = toolbar->objectName().toUtf8();
00304 toolbar->setVisible(hPref->GetBool(toolbarName.constData(), toolbar->isVisible()));
00305 }
00306 }
00307 }
00308
00309 void ToolBarManager::retranslate() const
00310 {
00311 QList<QToolBar*> toolbars = toolBars();
00312 for (QList<QToolBar*>::Iterator it = toolbars.begin(); it != toolbars.end(); ++it) {
00313 QByteArray toolbarName = (*it)->objectName().toUtf8();
00314 (*it)->setWindowTitle(
00315 QApplication::translate("Workbench",
00316 (const char*)toolbarName,
00317 0, QApplication::UnicodeUTF8));
00318 }
00319 }
00320
00321 QToolBar* ToolBarManager::findToolBar(const QList<QToolBar*>& toolbars, const QString& item) const
00322 {
00323 for (QList<QToolBar*>::ConstIterator it = toolbars.begin(); it != toolbars.end(); ++it) {
00324 if ((*it)->objectName() == item)
00325 return *it;
00326 }
00327
00328 return 0;
00329 }
00330
00331 QAction* ToolBarManager::findAction(const QList<QAction*>& acts, const QString& item) const
00332 {
00333 for (QList<QAction*>::ConstIterator it = acts.begin(); it != acts.end(); ++it) {
00334 if ((*it)->data().toString() == item)
00335 return *it;
00336 }
00337
00338 return 0;
00339 }
00340
00341 QList<QToolBar*> ToolBarManager::toolBars() const
00342 {
00343 QWidget* mw = getMainWindow();
00344 QList<QToolBar*> tb;
00345 QList<QToolBar*> bars = getMainWindow()->findChildren<QToolBar*>();
00346 for (QList<QToolBar*>::ConstIterator it = bars.begin(); it != bars.end(); ++it) {
00347 if ((*it)->parentWidget() == mw)
00348 tb.push_back(*it);
00349 }
00350
00351 return tb;
00352 }