src/gui/styles/qwindowsstyle.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
   113 
   113 
   114 QT_BEGIN_INCLUDE_NAMESPACE
   114 QT_BEGIN_INCLUDE_NAMESPACE
   115 #include <limits.h>
   115 #include <limits.h>
   116 QT_END_INCLUDE_NAMESPACE
   116 QT_END_INCLUDE_NAMESPACE
   117 
   117 
   118 static const int windowsItemFrame        =  2; // menu item frame width
       
   119 static const int windowsSepHeight        =  9; // separator item height
       
   120 static const int windowsItemHMargin      =  3; // menu item hor text margin
       
   121 static const int windowsItemVMargin      =  2; // menu item ver text margin
       
   122 static const int windowsArrowHMargin     =  6; // arrow horizontal margin
       
   123 static const int windowsRightBorder      = 15; // right border on windows
       
   124 static const int windowsCheckMarkWidth   = 12; // checkmarks width on windows
       
   125 
       
   126 enum QSliderDirection { SlUp, SlDown, SlLeft, SlRight };
   118 enum QSliderDirection { SlUp, SlDown, SlLeft, SlRight };
   127 
   119 
   128 /*
   120 /*
   129     \internal
   121     \internal
   130 */
   122 */
   219         break;
   211         break;
   220 #ifndef QT_NO_PROGRESSBAR
   212 #ifndef QT_NO_PROGRESSBAR
   221     case QEvent::StyleChange:
   213     case QEvent::StyleChange:
   222     case QEvent::Show:
   214     case QEvent::Show:
   223         if (QProgressBar *bar = qobject_cast<QProgressBar *>(o)) {
   215         if (QProgressBar *bar = qobject_cast<QProgressBar *>(o)) {
   224             d->bars << bar;
   216             if (!d->bars.contains(bar)) {
   225             if (d->bars.size() == 1) {
   217                 d->bars << bar;
   226                 Q_ASSERT(d->animationFps> 0);
   218                 if (d->bars.size() == 1) {
   227                 d->animateTimer = startTimer(1000 / d->animationFps);
   219                     Q_ASSERT(d->animationFps> 0);
       
   220                     d->animateTimer = startTimer(1000 / d->animationFps);
       
   221                 }
   228             }
   222             }
   229         }
   223         }
   230         break;
   224         break;
   231     case QEvent::Destroy:
   225     case QEvent::Destroy:
   232     case QEvent::Hide:
   226     case QEvent::Hide:
   452 
   446 
   453     case PM_IconViewIconSize:
   447     case PM_IconViewIconSize:
   454         ret = proxy()->pixelMetric(PM_LargeIconSize, opt, widget);
   448         ret = proxy()->pixelMetric(PM_LargeIconSize, opt, widget);
   455         break;
   449         break;
   456 
   450 
   457     case PM_ToolBarIconSize:
       
   458         ret = int(QStyleHelper::dpiScaled(24.));
       
   459         break;
       
   460     case PM_DockWidgetTitleMargin:
   451     case PM_DockWidgetTitleMargin:
   461         ret = int(QStyleHelper::dpiScaled(2.));
   452         ret = int(QStyleHelper::dpiScaled(2.));
   462         break;
   453         break;
   463     case PM_DockWidgetTitleBarButtonMargin:
   454     case PM_DockWidgetTitleBarButtonMargin:
   464         ret = int(QStyleHelper::dpiScaled(4.));
   455         ret = int(QStyleHelper::dpiScaled(4.));
  1848             bool checked = menuitem->checkType != QStyleOptionMenuItem::NotCheckable
  1839             bool checked = menuitem->checkType != QStyleOptionMenuItem::NotCheckable
  1849                             ? menuitem->checked : false;
  1840                             ? menuitem->checked : false;
  1850             bool act = menuitem->state & State_Selected;
  1841             bool act = menuitem->state & State_Selected;
  1851 
  1842 
  1852             // windows always has a check column, regardless whether we have an icon or not
  1843             // windows always has a check column, regardless whether we have an icon or not
  1853             int checkcol = qMax(menuitem->maxIconWidth, windowsCheckMarkWidth);
  1844             int checkcol = qMax<int>(menuitem->maxIconWidth, QWindowsStylePrivate::windowsCheckMarkWidth);
  1854 
  1845 
  1855             QBrush fill = menuitem->palette.brush(act ? QPalette::Highlight : QPalette::Button);
  1846             QBrush fill = menuitem->palette.brush(act ? QPalette::Highlight : QPalette::Button);
  1856             p->fillRect(menuitem->rect.adjusted(0, 0, -1, 0), fill);
  1847             p->fillRect(menuitem->rect.adjusted(0, 0, -1, 0), fill);
  1857 
  1848 
  1858             if (menuitem->menuItemType == QStyleOptionMenuItem::Separator){
  1849             if (menuitem->menuItemType == QStyleOptionMenuItem::Separator){
  1904                 newMi.state = State_None;
  1895                 newMi.state = State_None;
  1905                 if (!dis)
  1896                 if (!dis)
  1906                     newMi.state |= State_Enabled;
  1897                     newMi.state |= State_Enabled;
  1907                 if (act)
  1898                 if (act)
  1908                     newMi.state |= State_On;
  1899                     newMi.state |= State_On;
  1909                 newMi.rect = visualRect(opt->direction, menuitem->rect, QRect(menuitem->rect.x() + windowsItemFrame, menuitem->rect.y() + windowsItemFrame,
  1900                 newMi.rect = visualRect(opt->direction, menuitem->rect, QRect(menuitem->rect.x() + QWindowsStylePrivate::windowsItemFrame,
  1910                                                                               checkcol - 2 * windowsItemFrame, menuitem->rect.height() - 2*windowsItemFrame));
  1901                                                                               menuitem->rect.y() + QWindowsStylePrivate::windowsItemFrame,
       
  1902                                                                               checkcol - 2 * QWindowsStylePrivate::windowsItemFrame,
       
  1903                                                                               menuitem->rect.height() - 2 * QWindowsStylePrivate::windowsItemFrame));
  1911                 proxy()->drawPrimitive(PE_IndicatorMenuCheckMark, &newMi, p, widget);
  1904                 proxy()->drawPrimitive(PE_IndicatorMenuCheckMark, &newMi, p, widget);
  1912             }
  1905             }
  1913             p->setPen(act ? menuitem->palette.highlightedText().color() : menuitem->palette.buttonText().color());
  1906             p->setPen(act ? menuitem->palette.highlightedText().color() : menuitem->palette.buttonText().color());
  1914 
  1907 
  1915             QColor discol;
  1908             QColor discol;
  1916             if (dis) {
  1909             if (dis) {
  1917                 discol = menuitem->palette.text().color();
  1910                 discol = menuitem->palette.text().color();
  1918                 p->setPen(discol);
  1911                 p->setPen(discol);
  1919             }
  1912             }
  1920 
  1913 
  1921             int xm = windowsItemFrame + checkcol + windowsItemHMargin;
  1914             int xm = QWindowsStylePrivate::windowsItemFrame + checkcol + QWindowsStylePrivate::windowsItemHMargin;
  1922             int xpos = menuitem->rect.x() + xm;
  1915             int xpos = menuitem->rect.x() + xm;
  1923             QRect textRect(xpos, y + windowsItemVMargin, w - xm - windowsRightBorder - tab + 1, h - 2 * windowsItemVMargin);
  1916             QRect textRect(xpos, y + QWindowsStylePrivate::windowsItemVMargin,
       
  1917                            w - xm - QWindowsStylePrivate::windowsRightBorder - tab + 1, h - 2 * QWindowsStylePrivate::windowsItemVMargin);
  1924             QRect vTextRect = visualRect(opt->direction, menuitem->rect, textRect);
  1918             QRect vTextRect = visualRect(opt->direction, menuitem->rect, textRect);
  1925             QString s = menuitem->text;
  1919             QString s = menuitem->text;
  1926             if (!s.isEmpty()) {                     // draw text
  1920             if (!s.isEmpty()) {                     // draw text
  1927                 p->save();
  1921                 p->save();
  1928                 int t = s.indexOf(QLatin1Char('\t'));
  1922                 int t = s.indexOf(QLatin1Char('\t'));
  1952                 }
  1946                 }
  1953                 p->drawText(vTextRect, text_flags, s.left(t));
  1947                 p->drawText(vTextRect, text_flags, s.left(t));
  1954                 p->restore();
  1948                 p->restore();
  1955             }
  1949             }
  1956             if (menuitem->menuItemType == QStyleOptionMenuItem::SubMenu) {// draw sub menu arrow
  1950             if (menuitem->menuItemType == QStyleOptionMenuItem::SubMenu) {// draw sub menu arrow
  1957                 int dim = (h - 2 * windowsItemFrame) / 2;
  1951                 int dim = (h - 2 * QWindowsStylePrivate::windowsItemFrame) / 2;
  1958                 PrimitiveElement arrow;
  1952                 PrimitiveElement arrow;
  1959                 arrow = (opt->direction == Qt::RightToLeft) ? PE_IndicatorArrowLeft : PE_IndicatorArrowRight;
  1953                 arrow = (opt->direction == Qt::RightToLeft) ? PE_IndicatorArrowLeft : PE_IndicatorArrowRight;
  1960                 xpos = x + w - windowsArrowHMargin - windowsItemFrame - dim;
  1954                 xpos = x + w - QWindowsStylePrivate::windowsArrowHMargin - QWindowsStylePrivate::windowsItemFrame - dim;
  1961                 QRect  vSubMenuRect = visualRect(opt->direction, menuitem->rect, QRect(xpos, y + h / 2 - dim / 2, dim, dim));
  1955                 QRect  vSubMenuRect = visualRect(opt->direction, menuitem->rect, QRect(xpos, y + h / 2 - dim / 2, dim, dim));
  1962                 QStyleOptionMenuItem newMI = *menuitem;
  1956                 QStyleOptionMenuItem newMI = *menuitem;
  1963                 newMI.rect = vSubMenuRect;
  1957                 newMI.rect = vSubMenuRect;
  1964                 newMI.state = dis ? State_None : State_Enabled;
  1958                 newMI.state = dis ? State_None : State_Enabled;
  1965                 if (act)
  1959                 if (act)
  3192         if (const QStyleOptionMenuItem *mi = qstyleoption_cast<const QStyleOptionMenuItem *>(opt)) {
  3186         if (const QStyleOptionMenuItem *mi = qstyleoption_cast<const QStyleOptionMenuItem *>(opt)) {
  3193             int w = sz.width();
  3187             int w = sz.width();
  3194             sz = QCommonStyle::sizeFromContents(ct, opt, csz, widget);
  3188             sz = QCommonStyle::sizeFromContents(ct, opt, csz, widget);
  3195 
  3189 
  3196             if (mi->menuItemType == QStyleOptionMenuItem::Separator) {
  3190             if (mi->menuItemType == QStyleOptionMenuItem::Separator) {
  3197                 sz = QSize(10, windowsSepHeight);
  3191                 sz = QSize(10, QWindowsStylePrivate::windowsSepHeight);
  3198             }
  3192             }
  3199             else if (mi->icon.isNull()) {
  3193             else if (mi->icon.isNull()) {
  3200                 sz.setHeight(sz.height() - 2);
  3194                 sz.setHeight(sz.height() - 2);
  3201                 w -= 6;
  3195                 w -= 6;
  3202             }
  3196             }
  3203 
  3197 
  3204             if (mi->menuItemType != QStyleOptionMenuItem::Separator && !mi->icon.isNull()) {
  3198             if (mi->menuItemType != QStyleOptionMenuItem::Separator && !mi->icon.isNull()) {
  3205                 int iconExtent = proxy()->pixelMetric(PM_SmallIconSize, opt, widget);
  3199                 int iconExtent = proxy()->pixelMetric(PM_SmallIconSize, opt, widget);
  3206                 sz.setHeight(qMax(sz.height(),
  3200                 sz.setHeight(qMax(sz.height(),
  3207                                   mi->icon.actualSize(QSize(iconExtent, iconExtent)).height()
  3201                                   mi->icon.actualSize(QSize(iconExtent, iconExtent)).height()
  3208                                   + 2 * windowsItemFrame));
  3202                                   + 2 * QWindowsStylePrivate::windowsItemFrame));
  3209             }
  3203             }
  3210             int maxpmw = mi->maxIconWidth;
  3204             int maxpmw = mi->maxIconWidth;
  3211             int tabSpacing = 20;
  3205             int tabSpacing = 20;
  3212             if (mi->text.contains(QLatin1Char('\t')))
  3206             if (mi->text.contains(QLatin1Char('\t')))
  3213                 w += tabSpacing;
  3207                 w += tabSpacing;
  3214             else if (mi->menuItemType == QStyleOptionMenuItem::SubMenu)
  3208             else if (mi->menuItemType == QStyleOptionMenuItem::SubMenu)
  3215                 w += 2 * windowsArrowHMargin;
  3209                 w += 2 * QWindowsStylePrivate::windowsArrowHMargin;
  3216             else if (mi->menuItemType == QStyleOptionMenuItem::DefaultItem) {
  3210             else if (mi->menuItemType == QStyleOptionMenuItem::DefaultItem) {
  3217                 // adjust the font and add the difference in size.
  3211                 // adjust the font and add the difference in size.
  3218                 // it would be better if the font could be adjusted in the initStyleOption qmenu func!!
  3212                 // it would be better if the font could be adjusted in the initStyleOption qmenu func!!
  3219                 QFontMetrics fm(mi->font);
  3213                 QFontMetrics fm(mi->font);
  3220                 QFont fontBold = mi->font;
  3214                 QFont fontBold = mi->font;
  3221                 fontBold.setBold(true);
  3215                 fontBold.setBold(true);
  3222                 QFontMetrics fmBold(fontBold);
  3216                 QFontMetrics fmBold(fontBold);
  3223                 w += fmBold.width(mi->text) - fm.width(mi->text);
  3217                 w += fmBold.width(mi->text) - fm.width(mi->text);
  3224             }
  3218             }
  3225 
  3219 
  3226             int checkcol = qMax(maxpmw, windowsCheckMarkWidth); // Windows always shows a check column
  3220             int checkcol = qMax<int>(maxpmw, QWindowsStylePrivate::windowsCheckMarkWidth); // Windows always shows a check column
  3227             w += checkcol;
  3221             w += checkcol;
  3228             w += windowsRightBorder + 10;
  3222             w += QWindowsStylePrivate::windowsRightBorder + 10;
  3229             sz.setWidth(w);
  3223             sz.setWidth(w);
  3230         }
  3224         }
  3231         break;
  3225         break;
  3232 #endif // QT_NO_MENU
  3226 #endif // QT_NO_MENU
  3233 #ifndef QT_NO_MENUBAR
  3227 #ifndef QT_NO_MENUBAR
  3234     case CT_MenuBarItem:
  3228     case CT_MenuBarItem:
  3235         if (!sz.isEmpty())
  3229         if (!sz.isEmpty())
  3236             sz += QSize(windowsItemHMargin * 4, windowsItemVMargin * 2);
  3230             sz += QSize(QWindowsStylePrivate::windowsItemHMargin * 4, QWindowsStylePrivate::windowsItemVMargin * 2);
  3237         break;
  3231         break;
  3238 #endif
  3232 #endif
  3239                 // Otherwise, fall through
  3233                 // Otherwise, fall through
  3240     case CT_ToolButton:
  3234     case CT_ToolButton:
  3241         if (qstyleoption_cast<const QStyleOptionToolButton *>(opt))
  3235         if (qstyleoption_cast<const QStyleOptionToolButton *>(opt))