menutool.cc

来自「c++的guiQt做的开发」· CC 代码 · 共 997 行 · 第 1/3 页

CC
997
字号
ToolButton* MenuTool::createToolBarItem(ToolBar *tb,const QString &name,const QString &text,const QString &action, const QString &accel,const QString &icon, const QStringList &classes/*=QStringList()*/) { itemIcon.insert(name,icon); const QIcon *iconSet=cache->getIconSet(icon); QString tooltip=toolTipText(text,name,accel); ToolButton *tbutton=new ToolButton(iconSet,tooltip,tb); setAction(tbutton,action); addToMap(name,tbutton); if (!accel.isNull() && accel.length()>0) { //accelerator specified  if (reserveAccel(accel,action)) tbutton->setShortcut(QKeySequence(accel)); } tb->addButton(tbutton); tbutton->show(); if (classes.count()) { //Extra data - Item classes  for (QStringList::ConstIterator it=classes.begin();it!=classes.end();++it) {   addToMap("/"+*it,tbutton);  } } return tbutton;}/** Returns icon set with given name from cache (creating if necessary) Will emit a warning if icon is missing @param name Name of icon set to get @return specified Icon set*/const QIcon* MenuTool::getIconSet(const QString &name) { const QIcon* i=cache->getIconSet(name); if (!i) {  warn( QObject::tr("Icon missing: ") + name); } return i;}/** Returns icon with given name from cache (creating if necessary) Will emit a warning if icon is missing @param name Name of icon set to get @return specified Icon set*/const QPixmap* MenuTool::getIcon(const QString &name) { const QPixmap* i=cache->getIcon(name); if (!i) {  warn( QObject::tr("Icon missing: ") + name); } return i;}/** Load single toolbar from configuration file @param name Toolbar name in configuration file @param visible Will be toolbar initially visible? @return loaded toolbar*/ToolBar* MenuTool::loadToolbar(const QString &name,bool visible/*=true*/) throw (InvalidMenuException) { QString line=readItem(name); if (!isList(line)) { // List of values - first is name, others are items in it  invalidItem(QObject::tr("toolbar definition"),name,line,"list");  return NULL; } QString tbName=parseName(line,name); ToolBar *tb=new ToolBar(tbName,mainWindow); QObject::connect(tb,SIGNAL(itemClicked(ToolButton*)),this,SLOT(itemActivated(ToolButton*))); QObject::connect(tb,SIGNAL(helpText(const QString &)),this,SLOT(receiveHelpText(const QString &))); QObject::connect(this,SIGNAL(updateButtonSize(int)),tb,SLOT(setButtonSizes(int))); tb->setObjectName(name); //TODO: variable name if icon/size tb->setButtonSizes(globalSettings->readNum("icon/size",DEFAULT_TOOLBUTTON_SIZE)); QStringList qs=explode(MENULIST_SEPARATOR,line); QStringList::ConstIterator it=qs.begin(); for (;it!=qs.end();++it) { //load all subitems  loadToolBarItem(tb,*it); } if (visible) tb->show();  else        tb->hide(); mainWindow->addToolBar(tb); return tb;}/** Set new size of buttons in toolbars. Buttons are always square, function have no effect on other widgets in toolbars. @param sz new size of toolbar buttons*/void MenuTool::setButtonSize(int sz) { updateButtonSize(sz);}/** Load all toolbars from configuration files and add them to parent window If toolbar can't be loaded, InvalidMenuException will be thrown @param settingName name of key in settings used to store the configuration. If not specified, default is used.*/ToolBarList MenuTool::loadToolBars(const QString& settingName/*="gui/toolbars"*/) throw (InvalidMenuException) { QString line=globalSettings->read(settingName); if (line.isNull()) return toolbarList; //no toolbars! line=line.trimmed(); toolbarNames=explode(TOOLBARLIST_SEPARATOR,line); bool visible; for (int i=0;i<toolbarNames.count();i++) { //TODO configurable location in settings, save with the list?  visible=globalSettings->readBool(QString("toolbar/")+toolbarNames[i],true);  toolbarList[toolbarNames[i]]=loadToolbar(toolbarNames[i],visible); } return toolbarList;}/** Return list of toolbar names @return Toolbar list*/QStringList MenuTool::getToolbarList() { return toolbarNames;}/** Return toolbar with given name. @param name Name of toolbar @return Toolbar, or NULL if toolbar with given name was not found*/ToolBar* MenuTool::getToolbar(const QString &name) { if (!toolbarList.contains(name)) return NULL; return toolbarList[name];}/** Save toolbar state of QMainWindow to configuration @param settingName name of key in settings used to store the configuration. If not specified, default is used.*/void MenuTool::saveToolbars(const QString& settingName/*="gui/toolbarpos"*/) { QByteArray ba=mainWindow->saveState(); globalSettings->write(settingName,QString(ba.toBase64()));}/** Restore toolbar state of QMainWindow from configuration*/void MenuTool::restoreToolbars(const QString& settingName/*="gui/toolbarpos"*/) { QString out=globalSettings->read(settingName); if (out.isNull()) return; mainWindow->restoreState(QByteArray::fromBase64(out.toAscii()));}/** Add specified toolbar item to "all items" map @param name Name of item @param item The toolbutton or widget */void MenuTool::addToMap(const QString &name,QWidget* item) { mapTool.insert(name,item);}/** Add specified menu item to "all items" map @param name Name of item @param parent Parent menu @param itemId Id of item in parent menu*/void MenuTool::addToMap(const QString &name,QMenu* parent,QAction *itemId) { MenuItemsValue v(parent,itemId); mapMenu.insert(name,v);}/** Enable or disable item in toolbar and/or menu, given its name @param name Name of item @param enable True to enable, false to disable*/void MenuTool::enableByName(const QString &name,bool enable) { QList<QWidget*> tbItems=mapTool.values(name); for(int i=0;i<tbItems.size();i++) {  QWidget* el=tbItems.at(i);  el->setEnabled(enable); } QList<MenuItemsValue> menuItems=mapMenu.values(name); for(int i=0;i<menuItems.size();i++) {  MenuItemsValue el=menuItems.at(i);  QAction *id=el.second;  id->setEnabled(enable); }}/** Check or uncheck item in toolbar and/or menu, given its name<br> Note: Toolbuttons will automatically convert to Togglable toolbuttons this way       and will start togling itself automatically on each succesive click @param name Name of item @param check True to check, false to uncheck*/void MenuTool::checkByName(const QString &name,bool check) { QList<QWidget*> tbItems=mapTool.values(name); for(int i=0;i<tbItems.size();i++) {  ToolButton* el=dynamic_cast<ToolButton*>(tbItems.at(i));  if (el) {   el->setCheckable(true);   el->setChecked(check);  } } QList<MenuItemsValue> menuItems=mapMenu.values(name); for(int i=0;i<menuItems.size();i++) {  MenuItemsValue el=menuItems.at(i);  QAction *id=el.second;  id->setCheckable(true);  id->setChecked(check); }}/** Set text on toolbar or menu item with given name. If both toolbar and menu item exist with same name, both are updated. Note: the text will be translated according to the translation file, so english text should be used and any translation should be put into the translation file @param name Name of the item @param newText Text for the item*/void MenuTool::setTextByName(const QString &name,const QString &newText) { QList<QWidget*> tbItems=mapTool.values(name); for(int i=0;i<tbItems.size();i++) {  ToolButton* el=dynamic_cast<ToolButton*>(tbItems.at(i));  if (el) {   el->setTextLabel(toolTipText(newText,name));  } } QList<MenuItemsValue> menuItems=mapMenu.values(name); for(int i=0;i<menuItems.size();i++) {  MenuItemsValue el=menuItems.at(i);  QMenu* md=el.first;  QAction *id=el.second;  id->setText(menuItemText(newText,name));  //TODO: flag to avoid optimizing + reload does mass-optimize at end  if (md) optimizeItems(md); }}/** Set icon on toolbar or menu item with given name. If both toolbar and menu item exist with same name, both are updated. @param name Name of the item @param newIcon Name of new icon for the item*/void MenuTool::setIconByName(const QString &name,const QString &newIcon) { itemIcon.insert(name,newIcon); QList<QWidget*> tbItems=mapTool.values(name); for(int i=0;i<tbItems.size();i++) {  ToolButton* el=dynamic_cast<ToolButton*>(tbItems.at(i));  if (el) {   el->setIcon(*getIconSet(newIcon));  } } QList<MenuItemsValue> menuItems=mapMenu.values(name); for(int i=0;i<menuItems.size();i++) {  MenuItemsValue el=menuItems.at(i);  QAction *id=el.second;  id->setIcon(*getIconSet(newIcon)); }}/** Given name of toolbar or menu item, return its text This will return original (untranslated) text (in english) Setting it back with setTextByName will translate it according to the translation file @param name Name of the item @return text on the item*/QString MenuTool::getTextByName(const QString &name) { if (itemText.contains(name)) return itemText[name]; return QString::null;}/** Show or hide item in toolbar, given its name.<br> Items in menu can't be show or hidden this way @param name Name of item @param show True to show, false to hide*/void MenuTool::showByName(const QString &name,bool show) { QList<QWidget*> tbItems=mapTool.values(name); for(int i=0;i<tbItems.size();i++) {  QWidget* el=tbItems.at(i);  if (show) el->show(); else el->hide(); }}/** Reload all menu and toolbar items, when language or icon set changes.*/void MenuTool::reload() { QStringList rItems=itemText.keys(); for(int i=0;i<rItems.size();i++) {  const QString &id=rItems[i];  setTextByName(id,itemText[id]); } //Assume icon cache got already flushed QStringList iItems=itemIcon.keys(); for(int i=0;i<iItems.size();i++) {  const QString &id=iItems[i];  setIconByName(id,itemIcon[id]); }}/** Reload icon cache config and flush contents of icon cache*/void MenuTool::reloadIconCache() { cache->reload();}/** Reset entire state of this class, remove created menus and toolbars*/void MenuTool::reset() { delete menubar; for (ToolBarList::Iterator toolbar=toolbarList.begin();toolbar!=toolbarList.end();++toolbar) {  delete *toolbar; } action_items_map.clear(); toolbarNames.clear(); toolbarList.clear(); accels.clear(); itemText.clear(); itemIcon.clear(); accelText.clear(); mCache.clear(); popups.clear(); mAccel.clear(); mapMenu.clear(); mapTool.clear();}} // namespace gui

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?