#include "topquicktoolbarv2.h" #include #include #include #include #include #include "topquickbuttonv2.h" #include #include #include // ToolBarIconStyle_BigIconNotText , ToolBarIconStyle_LittleIconBesideText 当前在使用 const int IconSizeLst[TopQuickToolBarV2::ToolBarIconStyle_Count][2] = { 30,30, 32,30, 28,28, 20,20, 20,20, 28,28, 20,20}; const int BtnSizeLst[TopQuickToolBarV2::ToolBarIconStyle_Count][2] = { 60,60, 32,32, 120,60, 20,20, 20,20, 120,60, 20,20}; class TopQuickToolBarV2Private { Q_DECLARE_PUBLIC(TopQuickToolBarV2) public: explicit TopQuickToolBarV2Private(TopQuickToolBarV2 *qptr) : languageStr(APP->language()), mRightClickedMenu(NULL), toobtnStyle(Qt::ToolButtonTextBesideIcon), iconSize(QSize(TTHEME_DP(BtnSizeLst[TopQuickToolBarV2::ToolBarIconStyle_MidIconUnderText][0]), TTHEME_DP(BtnSizeLst[TopQuickToolBarV2::ToolBarIconStyle_MidIconUnderText][1]))), btnSize(QSize(TTHEME_DP(IconSizeLst[TopQuickToolBarV2::ToolBarIconStyle_MidIconUnderText][0]), TTHEME_DP(IconSizeLst[TopQuickToolBarV2::ToolBarIconStyle_MidIconUnderText][1]))), q_ptr(qptr) { } ~TopQuickToolBarV2Private() { } QString languageStr; QActionGroup *mToolBarActions; QActionGroup *actionGroups; QMenu *mRightClickedMenu; bool mToolBtnHasTextBol; int mToolBarIconStyleInt; Qt::ToolButtonStyle toobtnStyle; QSize iconSize; QSize btnSize; protected: TopQuickToolBarV2 * const q_ptr; }; TopQuickToolBarV2::TopQuickToolBarV2(QWidget *parent) : QToolBar(parent), d_ptr(new TopQuickToolBarV2Private(this)) { Q_D(TopQuickToolBarV2); setContextMenuPolicy(Qt::CustomContextMenu); connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onCustomContextMenuRequested(QPoint))); d->mToolBarActions = new QActionGroup(this); d->actionGroups = new QActionGroup(this); connect(d->actionGroups, SIGNAL(triggered(QAction*)), this, SLOT(onActionTriggered(QAction *))); connect(this, SIGNAL(iconStyleChanged(int)), this, SLOT(onExternalActionTriggered(int))); d->mRightClickedMenu = createMenu(); } TopQuickToolBarV2::~TopQuickToolBarV2() { } void TopQuickToolBarV2::setLanguage(const QString &iLangStr) { Q_D(TopQuickToolBarV2); d->languageStr = iLangStr; } QString TopQuickToolBarV2::language() const { Q_D(const TopQuickToolBarV2); return d->languageStr; } void TopQuickToolBarV2::loadQuickButtons(const QVariantList &iVarList) { foreach (QVariant var, iVarList) { addQuickButton(var.toMap()); } } TopQuickButtonV2 *TopQuickToolBarV2::addQuickButton(const QVariantMap &iParam) { Q_D(TopQuickToolBarV2); TopQuickButtonV2 *btn = createQuickBtn(iParam); btn->setToolButtonStyle(d->toobtnStyle); btn->setIconSize(d->iconSize); btn->setFixedSize(d->btnSize); d->mToolBarActions->addAction(addWidget(btn)); return btn; } void TopQuickToolBarV2::setToolButtonStyleSize(int iconStyleInt) { Q_D(TopQuickToolBarV2); d->mToolBarIconStyleInt = iconStyleInt; switch (iconStyleInt) { case ToolBarIconStyle_BigIconNotText: // 大图标 不含文字 d->mToolBtnHasTextBol = false; setToolBtnIconStyle(Qt::ToolButtonIconOnly, iconStyleInt); break; case ToolBarIconStyle_MidIconNoText: // 中图标 不含文字 d->mToolBtnHasTextBol = false; setToolBtnIconStyle(Qt::ToolButtonIconOnly, iconStyleInt); break; case ToolBarIconStyle_MidIconUnderText: // 中图标 含文字(下方) d->mToolBtnHasTextBol = true; setToolBtnIconStyle(Qt::ToolButtonTextUnderIcon, iconStyleInt); break; case ToolBarIconStyle_LittleIconNotText: // 小图标 不含文字 d->mToolBtnHasTextBol = false; setToolBtnIconStyle(Qt::ToolButtonIconOnly, iconStyleInt); break; case ToolBarIconStyle_LittleIconUnderText: // 小图标 含文字(下方) d->mToolBtnHasTextBol = true; setToolBtnIconStyle(Qt::ToolButtonTextUnderIcon, iconStyleInt); break; case ToolBarIconStyle_LittleIconBesideText: // 小图标 含文字(旁边) d->mToolBtnHasTextBol = true; setToolBtnIconStyle(Qt::ToolButtonTextBesideIcon, iconStyleInt); break; case ToolBarIconStyle_NoIconOnlyText: // 只有文字 不含图标 d->mToolBtnHasTextBol = true; setToolBtnIconStyle(Qt::ToolButtonTextOnly, iconStyleInt); break; default: break; } } void TopQuickToolBarV2::initToolButtonStyleSize(int iconStyleInt) { Q_D(TopQuickToolBarV2); setToolButtonStyleSize(iconStyleInt); foreach (QAction *act, d->actionGroups->actions()) { if (act->data().toInt() == iconStyleInt) { act->setChecked(true); act->setIcon(TRES->icon("ok.white")); } } } int TopQuickToolBarV2::toolBarBtnIconStyle() { Q_D(TopQuickToolBarV2); return d->mToolBarIconStyleInt; } void TopQuickToolBarV2::clearAllButtons() { Q_D(TopQuickToolBarV2); // 清除当前ACTION,会自动析构对应的QToolButoon QList listAction = d->mToolBarActions->actions(); foreach (QAction *act, listAction) { d->mToolBarActions->removeAction(act); this->removeAction(act); act->deleteLater(); } } Qt::ToolButtonStyle TopQuickToolBarV2::BtnStyle() const { Q_D(const TopQuickToolBarV2); return d->toobtnStyle; } QSize TopQuickToolBarV2::BtnIconSize() const { Q_D(const TopQuickToolBarV2); return d->iconSize; } QSize TopQuickToolBarV2::sizeHint() { Q_D(const TopQuickToolBarV2); this->setMinimumSize(d->btnSize); this->resize(d->btnSize); emit sizeChanged(d->btnSize); return d->btnSize; } QString TopQuickToolBarV2::textConvertion(const QString &iTextStr) { QString itest = iTextStr.trimmed(); int zhPerLength = 12; // fang 4 int enUpperPerLength = 8; // x6 int enLowerPerLength = 6; // x8 int totalRowLength = 48; int curRowLength = 0; for (int i = 0; i < itest.count(); i++) { QChar cha = itest.at(i); ushort uni = cha.unicode(); int curCharLength = 0; if (uni >= 0x4E00 && uni < 0x9FA5) { curCharLength = zhPerLength; } else if (uni >= 65 && uni <= 90) { curCharLength = enUpperPerLength; } else { curCharLength = enLowerPerLength; } curRowLength += curCharLength; if (curRowLength == totalRowLength) { curRowLength = 0; itest.insert(i+1, "\n "); i+=2; } else if (curRowLength > totalRowLength && (curRowLength - totalRowLength) > curCharLength / 2 ) // 长度超过,必须超过半个字的长度才换行 { curRowLength = curCharLength; itest.insert(i, "\n "); i+=2; } } if (itest.endsWith("\n ")) { itest.remove(itest.lastIndexOf("\n "), 2); } itest.prepend(" "); return itest; } void TopQuickToolBarV2::onCustomContextMenuRequested(QPoint) { Q_D(TopQuickToolBarV2); if (!d->mRightClickedMenu) { d->mRightClickedMenu = createMenu(); } } void TopQuickToolBarV2::onActionTriggered(QAction *iAction) { Q_D(TopQuickToolBarV2); if (!iAction || !iAction->data().isValid()) return; int dataInt = iAction->data().toInt(); iAction->setChecked(true); foreach(QAction *act, d->actionGroups->actions()) { if (act != iAction) { act->setChecked(false); act->setIcon(TRES->icon("")); } else { act->setIcon(TRES->icon("ok.white")); act->setChecked(true); } } setToolButtonStyleSize(dataInt); } void TopQuickToolBarV2::onExternalActionTriggered(int iconStyleInt) { setToolButtonStyleSize(iconStyleInt); } QMenu *TopQuickToolBarV2::createMenu() { Q_D(TopQuickToolBarV2); QMenu *menu = new QMenu(this); QAction *act0 = new QAction("Big Icon", this); act0->setData(0); QAction *act1 = new QAction("Mid Icon", this); act1->setData(1); QAction *act2 = new QAction("Mid Icon (UnderText)", this); act2->setData(2); QAction *act3 = new QAction("Little Icon", this); act3->setData(3); QAction *act4 = new QAction("Little Icon (Under Text)", this); act4->setData(4); QAction *act5 = new QAction("Little Icon (BesideText)", this); act5->setData(5); QAction *act6 = new QAction("Only Text", this); act6->setData(6); QActionGroup *actionGroups = d->actionGroups; //clear QList actionLst = actionGroups->actions(); foreach (QAction *act, actionLst) { actionGroups->removeAction(act); act->deleteLater(); } // add actionGroups->addAction(act0); actionGroups->addAction(act1); actionGroups->addAction(act2); actionGroups->addAction(act3); actionGroups->addAction(act4); actionGroups->addAction(act5); actionGroups->addAction(act6); foreach (QAction *act, actionGroups->actions()) { act->setCheckable(true); } menu->addActions(actionGroups->actions()); return menu; } void TopQuickToolBarV2::setToolBtnIconStyle(Qt::ToolButtonStyle iIconStyle, int iIconStyleInt) { Q_D(TopQuickToolBarV2); d->toobtnStyle = iIconStyle; d->iconSize = QSize(TTHEME_DP(IconSizeLst[iIconStyleInt][0]), TTHEME_DP(IconSizeLst[iIconStyleInt][1])); d->btnSize = QSize(TTHEME_DP(BtnSizeLst[iIconStyleInt][0]), TTHEME_DP(BtnSizeLst[iIconStyleInt][1])); this->setToolButtonStyle(iIconStyle); this->setIconSize(d->iconSize); emit toolBarButtonReloaded(iIconStyle, d->btnSize, d->iconSize); } TopQuickButtonV2 *TopQuickToolBarV2::createQuickBtn(const QVariantMap &iParam) { Q_D(TopQuickToolBarV2); TopQuickButtonV2 *toolBtn = new TopQuickButtonV2(this); QString btnTextStr = iParam.value("title_" + d->languageStr).toString(); if (btnTextStr.isEmpty()) { btnTextStr = iParam.value("title").toString(); } QString btnTooltipStr = iParam.value("remark_" + d->languageStr).toString(); if (btnTooltipStr.isEmpty()) { btnTooltipStr = iParam.value("remark").toString(); } if (btnTooltipStr.isEmpty()) { btnTooltipStr = btnTextStr; } QString iconStr = iParam.value("icon").toString(); if (!iconStr.isEmpty() && !iconStr.contains('.')) { iconStr += ".#FFFFFFAF"; } toolBtn->setText(textConvertion(btnTextStr)); toolBtn->setToolTip(btnTooltipStr); if (!iconStr.isEmpty()) { toolBtn->setIcon(TRES->icon(iconStr)); } QString type = iParam.value("type").toString().toUpper(); if (type == "SHORTCUT") { toolBtn->setType(TopQuickButtonV2::Type_Shortcut); QVariantMap dataMap = TDataParse::commandStr2Map(iParam.value("urlAddress").toString()); toolBtn->setUid(dataMap.value("COM").toString().toUpper() + "/" + dataMap.value("UID").toString()); } else if (type == "GROUP") { toolBtn->setType(TopQuickButtonV2::Type_Group); } else if (type == "ACTION") { toolBtn->setType(TopQuickButtonV2::Type_Action); } if (iParam.value("fromDb").toString() == "sys") { toolBtn->setUnfixable(false); } else { toolBtn->setUnfixable(true); } toolBtn->setData(iParam); return toolBtn; }