KDEUI
kstyle.h
Go to the documentation of this file.
00001 00035 #ifndef KDE_KSTYLE_H 00036 #define KDE_KSTYLE_H 00037 00038 #include <kdeui_export.h> 00039 00040 #include <QtGui/QCommonStyle> 00041 #include <QtGui/QPalette> 00042 #include <QtGui/QStylePlugin> 00043 #include <typeinfo> 00044 00045 class QStyleOptionProgressBar; 00046 class QStyleOptionTab; 00047 00048 class KStylePrivate; 00074 // TODO: From 'Qt4 Themes' discussion on kde-devel 00075 // - Remi Villatel: extend QStyle enums for KColorButton, KColorCombo, KKeyButton, split PE_HeaderSection into KPopupTitle, PopupMenuTitle, TaskContainer) 00076 // - RV: KLineEdit "plays with its colors" - related to KStyle? 00077 // - RV: KMulitTabBarTab ... does it need support from KStyle (instead of manual button rotation etc.)? Or is it dumped already? 00078 // - RV: KTabCtl draws itself mimicking QDrawShadeThingies 00079 // - RV: fixed colors (e.g. Konqueror, KToolbarButton label text ->KPE_ToolbarButton?): To prevent hacks like "preventing any PaletteChange()"... mor related to KDE4 color schemes... I guess 00080 // - LM: User interface guidelines... related to KStyle? 00081 // - e.g. drawFancyPE() in kdeui for KDE widgets: check "qobject_cast<KStyle*>(style())", or fallback drawing... 00082 // TODO: implement standardIcon().. and what about standardPalette()? 00083 // TODO: maybe the arrow in CE_PushButtonLabel should be painted in CE_PushButtonBevel like QCommonStyle 00084 00085 class KDEUI_EXPORT KStyle: public QCommonStyle 00086 { 00087 Q_OBJECT 00088 00089 public: 00090 KStyle(); 00091 ~KStyle(); 00092 00096 static QString defaultStyle(); 00123 static StyleHint customStyleHint(const QString &element, const QWidget *widget); 00124 static ControlElement customControlElement(const QString &element, const QWidget *widget); 00125 static SubElement customSubElement(const QString &element, const QWidget *widget); 00126 00127 protected: 00128 00157 StyleHint newStyleHint(const QString &element); 00158 ControlElement newControlElement(const QString &element); 00159 SubElement newSubElement(const QString &element); 00160 00168 void drawInsideRect(QPainter* p, const QRect& r) const; 00169 00173 QRect centerRect(const QRect &in, int w, int h) const; 00174 00178 QRect centerRect(const QRect &in, const QSize &size) const; 00180 00191 class KDEUI_EXPORT ColorMode //krazy:exclude=dpointer (lightweight helper) 00192 { 00193 public: 00201 enum Mode 00202 { 00203 PaletteEntryMode, 00204 BWAutoContrastMode = 0x8000000 00205 }; 00206 private: 00207 Mode mode; 00208 QPalette::ColorRole role; 00209 public: 00210 00213 ColorMode(QPalette::ColorRole _role); 00214 00216 ColorMode(Mode _mode, QPalette::ColorRole _role); 00217 00219 operator int() const; 00220 00222 ColorMode(int encoded); 00223 00226 QColor color(const QPalette& palette); 00227 }; 00228 00229 00239 struct KDEUI_EXPORT Option 00240 { 00241 virtual ~Option() {} //So dynamic_cast works, and g++ shuts up 00242 }; 00243 00253 template<typename EventualSubtype, typename BaseType> 00254 struct KDEUI_EXPORT OptionBase: public BaseType 00255 { 00259 static EventualSubtype* defaultOption() 00260 { 00261 static EventualSubtype* theDefault = 0; //### function static, not very nice, 00262 //but avoids need for explicit instantiation. 00263 00264 if (!theDefault) 00265 theDefault = new EventualSubtype; 00266 00267 return theDefault; 00268 } 00269 }; 00270 00277 template<typename T> 00278 static T extractOption(Option* option); 00279 00284 struct KDEUI_EXPORT ColorOption: public OptionBase<ColorOption, Option> 00285 { 00287 ColorMode color; 00288 00289 ColorOption(): color(QPalette::ButtonText) 00290 {} 00291 }; 00292 00297 struct KDEUI_EXPORT IconOption: public OptionBase<IconOption, Option> 00298 { 00299 bool active; 00300 QIcon icon; 00301 QSize size; 00302 00303 IconOption(): active(false) 00304 {} 00305 }; 00306 00312 struct KDEUI_EXPORT DoubleButtonOption: public OptionBase<DoubleButtonOption, Option> 00313 { 00317 enum ActiveButton 00318 { 00319 None, 00320 Top, 00321 Left, 00322 Right, 00323 Bottom 00324 }; 00325 00330 ActiveButton activeButton; 00331 00332 DoubleButtonOption(): activeButton(None) 00333 {} 00334 00340 DoubleButtonOption(ActiveButton ab): activeButton(ab) 00341 {} 00342 }; 00343 00349 struct KDEUI_EXPORT TitleButtonOption: public OptionBase<TitleButtonOption, Option> 00350 { 00351 bool active; 00352 QIcon icon; 00353 // /// whether the button is hovered, this doesn't work at the moment (not even in any Qt style)... 00354 // bool hover; 00355 00356 TitleButtonOption(): active(false)/*, hover(false)*/ 00357 {} 00358 00364 TitleButtonOption(bool act): active(act) 00365 {} 00366 }; 00367 00369 struct KDEUI_EXPORT TextOption: public OptionBase<TextOption, ColorOption> 00370 { 00371 Qt::Alignment hAlign; 00372 QString text; 00373 00374 TextOption(); 00375 00381 TextOption(const QString& _text); 00382 00386 void init(); 00387 }; 00389 00401 enum WidgetType 00402 { 00403 WT_Generic, 00404 WT_PushButton, 00405 WT_Splitter, 00406 WT_CheckBox, 00407 WT_RadioButton, 00408 WT_DockWidget, 00409 WT_ProgressBar, 00410 WT_MenuBar, 00411 WT_MenuBarItem, 00412 WT_Menu, 00413 WT_MenuItem, 00414 WT_ScrollBar, 00415 WT_TabBar, 00416 WT_TabWidget, 00417 WT_Slider, 00418 WT_Tree, 00419 WT_SpinBox, 00420 WT_ComboBox, 00421 WT_Header, 00422 WT_LineEdit, 00423 WT_GroupBox, 00424 WT_StatusBar, 00425 WT_ToolBar, 00426 WT_ToolButton, 00427 WT_ToolBoxTab, 00428 WT_Window, 00429 WT_Limit = 0xFFFF 00430 }; 00431 00432 00440 enum MarginOffsets 00441 { 00442 MainMargin, 00445 Top, 00446 Bot, 00447 Left, 00448 Right, 00449 MarginInc 00451 }; 00452 00453 00458 struct Generic 00459 { 00465 enum LayoutProp 00466 { 00467 DefaultFrameWidth, 00468 DefaultLayoutSpacing, 00469 DefaultLayoutMargin 00470 }; 00471 00477 enum Primitive 00478 { 00479 Text = 0xFFFF, 00480 Icon, 00481 FocusIndicator, 00482 Frame, 00485 ArrowUp, 00486 ArrowDown, 00487 ArrowRight, 00488 ArrowLeft 00489 }; 00490 }; 00491 00497 struct PushButton 00498 { 00511 enum LayoutProp 00512 { 00513 ContentsMargin, 00514 FocusMargin = ContentsMargin + MarginInc, 00515 DefaultIndicatorMargin = FocusMargin + MarginInc, 00516 PressedShiftHorizontal = DefaultIndicatorMargin + MarginInc, 00517 PressedShiftVertical, 00518 MenuIndicatorSize, 00519 TextToIconSpace 00520 }; 00521 00532 enum Primitive 00533 { 00534 Panel, 00536 DefaultButtonFrame 00539 }; 00540 }; 00541 00547 struct Splitter 00548 { 00552 enum LayoutProp 00553 { 00554 Width 00555 }; 00556 00562 enum Primitive 00563 { 00564 HandleHor, 00565 HandleVert 00566 }; 00567 }; 00568 00574 struct CheckBox 00575 { 00579 enum LayoutProp 00580 { 00581 Size, 00582 BoxTextSpace, 00583 NoLabelFocusMargin, 00586 FocusMargin = NoLabelFocusMargin + MarginInc 00587 }; 00588 00601 enum Primitive 00602 { 00603 CheckOn, 00604 CheckOff, 00605 CheckTriState 00606 }; 00607 }; 00608 00612 struct RadioButton 00613 { 00617 enum LayoutProp 00618 { 00619 Size, 00622 BoxTextSpace, 00623 FocusMargin 00624 }; 00625 00637 enum Primitive 00638 { 00639 RadioOn, 00640 RadioOff 00641 }; 00642 }; 00643 00644 00650 struct DockWidget 00651 { 00655 enum LayoutProp 00656 { 00657 TitleTextColor, 00658 TitleMargin, 00659 FrameWidth = TitleMargin + MarginInc, 00660 SeparatorExtent 00661 }; 00662 00672 enum Primitive 00673 { 00674 TitlePanel, 00675 SeparatorHandle 00676 }; 00677 }; 00678 00686 struct ProgressBar 00687 { 00691 enum LayoutProp 00692 { 00693 GrooveMargin, 00694 SideText = GrooveMargin + MarginInc, 00695 SideTextSpace, 00696 Precision, 00697 BusyIndicatorSize, 00698 MaxBusyIndicatorSize 00699 }; 00700 00707 enum Primitive 00708 { 00709 Groove, 00711 Indicator, 00712 BusyIndicator 00715 }; 00716 }; 00717 00718 00724 struct MenuBar 00725 { 00729 enum LayoutProp 00730 { 00731 Margin, 00733 ItemSpacing = Margin + MarginInc 00734 }; 00735 00739 enum Property 00740 { 00741 EmptyArea 00744 }; 00745 }; 00746 00747 00754 struct MenuBarItem 00755 { 00759 enum LayoutProp 00760 { 00761 Margin, 00764 Dummy = Margin + MarginInc //Paranoia about underlying type 00765 }; 00766 00770 enum Primitive 00771 { 00772 Panel 00773 }; 00774 }; 00775 00781 struct Menu 00782 { 00786 enum LayoutProp 00787 { 00788 FrameWidth, 00789 Margin, 00790 ScrollerHeight = Margin + MarginInc, 00791 TearOffHeight 00792 }; 00793 00800 enum Primitive 00801 { 00802 Background, 00803 TearOff, 00805 Scroller 00807 }; 00808 }; 00809 00828 struct MenuItem 00829 { 00833 enum LayoutProp 00834 { 00835 Margin, 00836 CheckAlongsideIcon = Margin + MarginInc, 00839 CheckWidth, 00841 CheckSpace, 00843 IconWidth, 00844 IconSpace, 00845 AccelSpace, 00846 ArrowSpace, 00847 ArrowWidth, 00848 SeparatorHeight, 00849 MinHeight, 00850 ActiveTextColor, 00851 TextColor, 00852 DisabledTextColor, 00853 ActiveDisabledTextColor 00854 }; 00855 00863 enum Primitive 00864 { 00865 CheckColumn, 00866 CheckOn, 00867 CheckOff, 00868 RadioOn, 00869 RadioOff, 00870 CheckIcon, 00871 Separator, //A separator item. 00872 ItemIndicator //Shows the active item 00873 }; 00874 }; 00875 00876 00882 struct ScrollBar 00883 { 00890 enum LayoutProp 00891 { 00892 DoubleTopButton, 00893 DoubleBotButton, 00894 SingleButtonHeight, 00895 DoubleButtonHeight, 00896 BarWidth, 00897 MinimumSliderHeight, 00900 ArrowColor, 00902 ActiveArrowColor 00903 }; 00904 00914 enum Primitive 00915 { 00916 SingleButtonVert, 00917 SingleButtonHor, 00918 DoubleButtonVert, 00921 DoubleButtonHor, 00922 GrooveAreaVertBottom, 00923 GrooveAreaHorRight, 00924 GrooveAreaVertTop, 00925 GrooveAreaHorLeft, 00926 SliderVert, 00927 SliderHor 00928 }; 00929 }; 00930 00936 struct TabBar 00937 { 00951 enum LayoutProp 00952 { 00953 TabContentsMargin, 00954 TabFocusMargin = TabContentsMargin + MarginInc, 00957 TabTextToIconSpace = TabFocusMargin + MarginInc, 00959 TabOverlap, 00964 BaseHeight, 00965 BaseOverlap, 00966 ScrollButtonWidth 00967 }; 00968 00976 enum Primitive 00977 { 00978 EastText, 00979 WestText, 00980 NorthTab, 00981 EastTab, 00982 WestTab, 00983 SouthTab, 00984 BaseFrame, 00985 IndicatorTear 00989 }; 00990 }; 00991 01000 struct TabWidget 01001 { 01005 enum LayoutProp 01006 { 01007 ContentsMargin, 01014 DummyProp = ContentsMargin+MarginInc 01015 }; 01016 }; 01017 01023 struct Slider 01024 { 01025 01031 enum LayoutProp 01032 { 01033 HandleThickness, 01034 HandleLength 01035 }; 01036 01043 enum Primitive 01044 { 01045 HandleVert, 01046 HandleHor, 01047 GrooveVert, 01048 GrooveHor 01049 }; 01050 }; 01051 01052 01056 struct Tree 01057 { 01067 enum LayoutProp 01068 { 01069 MaxExpanderSize 01073 }; 01074 01078 enum Primitive 01079 { 01080 ExpanderClosed, 01081 ExpanderOpen, 01082 HorizontalBranch, 01086 VerticalBranch 01087 }; 01088 }; 01089 01093 struct SpinBox 01094 { 01110 enum LayoutProp 01111 { 01112 FrameWidth, 01114 ButtonWidth, 01115 ButtonMargin, 01119 ButtonSpacing = ButtonMargin + MarginInc, 01120 SymmetricButtons, 01125 SupportFrameless, 01129 ContentsMargin 01132 }; 01133 01139 enum Primitive 01140 { 01141 EditField, 01144 UpButton, 01145 DownButton, 01146 ButtonArea, 01149 PlusSymbol, 01151 MinusSymbol 01153 }; 01154 }; 01155 01159 struct ComboBox 01160 { 01172 enum LayoutProp 01173 { 01174 FrameWidth, 01175 ButtonWidth, 01176 ButtonMargin, 01178 FocusMargin = ButtonMargin + MarginInc, 01181 SupportFrameless = FocusMargin + MarginInc, 01184 ContentsMargin 01187 }; 01188 01197 enum Primitive 01198 { 01199 EditField, 01200 Button 01201 }; 01202 }; 01203 01209 struct Header 01210 { 01214 enum LayoutProp 01215 { 01216 ContentsMargin, 01217 TextToIconSpace = ContentsMargin + MarginInc, 01221 MarkSize 01223 }; 01224 01232 enum Primitive 01233 { 01234 SectionHor, 01235 SectionVert 01236 }; 01237 }; 01238 01244 struct LineEdit 01245 { 01252 enum Primitive 01253 { 01254 Panel 01255 }; 01256 }; 01257 01270 struct GroupBox 01271 { 01275 enum LayoutProps 01276 { 01277 FrameWidth, 01278 TextAlignTop, 01281 TitleTextColor 01282 }; 01286 enum Primitive 01287 { 01288 FlatFrame 01291 }; 01292 }; 01293 01304 struct StatusBar 01305 { 01309 }; 01310 01316 struct ToolBar 01317 { 01321 enum LayoutProps 01322 { 01323 HandleExtent, 01324 SeparatorExtent, 01325 ExtensionExtent, 01326 FrameWidth, 01328 ItemMargin, 01329 ItemSpacing 01330 }; 01331 01338 enum Primitive 01339 { 01340 HandleHor, 01341 HandleVert, 01342 Separator, 01343 PanelHor, 01344 PanelVert 01345 }; 01346 }; 01347 01348 01354 struct ToolBoxTab 01355 { 01359 enum LayoutProps 01360 { 01361 Margin 01364 }; 01365 01369 enum Primitive 01370 { 01371 Panel 01374 }; 01375 }; 01376 01377 01388 struct ToolButton 01389 { 01393 enum LayoutProps 01394 { 01395 ContentsMargin, 01397 FocusMargin = ContentsMargin + MarginInc, 01400 MenuIndicatorSize, 01402 InlineMenuIndicatorSize = FocusMargin + MarginInc, 01405 InlineMenuIndicatorXOff, 01408 InlineMenuIndicatorYOff 01411 }; 01412 01423 enum Primitive 01424 { 01425 Panel 01427 }; 01428 }; 01429 01430 01438 struct Window 01439 { 01443 enum LayoutProps 01444 { 01445 TitleTextColor, 01446 TitleHeight, 01447 NoTitleFrame, 01450 TitleMargin, 01453 ButtonWidth = TitleMargin + MarginInc, 01454 ButtonSpace, 01455 ButtonToTextSpace 01456 }; 01457 01467 enum Primitive 01468 { 01469 TitlePanel, 01470 ButtonMenu, 01471 ButtonMin, 01472 ButtonMax, 01473 ButtonRestore, 01474 ButtonClose, 01475 ButtonShade, 01476 ButtonUnshade, 01477 ButtonHelp 01478 }; 01479 }; 01481 01483 void setWidgetLayoutProp(WidgetType widget, int metric, int value); 01484 01499 virtual int widgetLayoutProp(WidgetType widgetType, int metric, 01500 const QStyleOption* opt = 0, const QWidget* w = 0) const; 01501 01530 virtual void drawKStylePrimitive(WidgetType widgetType, int primitive, 01531 const QStyleOption* opt, 01532 const QRect &r, const QPalette &pal, 01533 State flags, QPainter* p, 01534 const QWidget* widget = 0, 01535 Option* kOpt = 0) const; 01536 private: 01538 bool useSideText(const QStyleOptionProgressBar* opt) const; 01539 int sideTextWidth(const QStyleOptionProgressBar* pbOpt) const; 01540 01542 bool isVerticalTab (const QStyleOptionTab* tbOpt) const; 01543 01545 bool isReflectedTab(const QStyleOptionTab* tbOpt) const; 01546 01547 enum Side 01548 { 01549 North, 01550 East, 01551 West, 01552 South 01553 }; 01554 01555 Side tabSide(const QStyleOptionTab* tbOpt) const; 01556 01558 QRect marginAdjustedTab(const QStyleOptionTab* tbOpt, int property) const; 01559 01561 QRect handleRTL(const QStyleOption* opt, const QRect& subRect) const; 01562 QPoint handleRTL(const QStyleOption* opt, const QPoint& pos) const; 01563 01565 QVector<QVector<int> > metrics; 01566 01568 QSize expandDim(const QSize& orig, WidgetType widget, int baseMarginMetric, const QStyleOption* opt, const QWidget* w, bool rotated = false) const; 01569 01572 QRect insideMargin(const QRect &orig, WidgetType widget, int baseMarginMetric, const QStyleOption* opt, const QWidget* w) const; 01573 01576 QRect internalSubControlRect (ComplexControl control, const QStyleOptionComplex* opt, 01577 SubControl subControl, const QWidget* w) const; 01578 01579 // fitt's law label support: QLabel focusing its buddy widget 01580 const QObject *clickedLabel; 01581 01582 template<typename T> 01583 static T extractOptionHelper(T*); 01584 01585 public: 01594 void drawControl (ControlElement elem, const QStyleOption* opt, QPainter* p, const QWidget* w) const; 01595 void drawPrimitive (PrimitiveElement elem, const QStyleOption* opt, QPainter* p, const QWidget* w) const; 01596 int pixelMetric (PixelMetric metric, const QStyleOption* opt = 0, const QWidget* w = 0) const; 01597 QRect subElementRect (SubElement subRect, const QStyleOption* opt, const QWidget* w) const; 01598 QSize sizeFromContents(ContentsType type, const QStyleOption* opt, 01599 const QSize& contentsSize, const QWidget* w) const; 01600 int styleHint (StyleHint hint, const QStyleOption* opt, const QWidget* w, 01601 QStyleHintReturn* returnData) const; 01602 QRect subControlRect (ComplexControl control, const QStyleOptionComplex* opt, 01603 SubControl subControl, const QWidget* w) const; 01604 SubControl hitTestComplexControl(ComplexControl cc, const QStyleOptionComplex* opt, 01605 const QPoint& pt, const QWidget* w) const; 01606 void drawComplexControl (ComplexControl cc, const QStyleOptionComplex* opt, 01607 QPainter *p, const QWidget* w) const; 01608 01609 void polish(QWidget *); 01610 void unpolish(QWidget *); 01611 void polish(QApplication *); 01612 void unpolish(QApplication *); 01613 void polish(QPalette &); 01614 QRect itemTextRect(const QFontMetrics &fm, const QRect &r, 01615 int flags, bool enabled, 01616 const QString &text) const; 01617 QRect itemPixmapRect(const QRect &r, int flags, const QPixmap &pixmap) const; 01618 void drawItemText(QPainter *painter, const QRect &rect, 01619 int flags, const QPalette &pal, bool enabled, 01620 const QString &text, QPalette::ColorRole textRole = QPalette::NoRole) const; 01621 void drawItemPixmap(QPainter *painter, const QRect &rect, 01622 int alignment, const QPixmap &pixmap) const; 01623 QPalette standardPalette() const; 01624 QPixmap standardPixmap(StandardPixmap standardPixmap, const QStyleOption *opt, 01625 const QWidget *widget = 0) const; //### kde5 remove 01626 QPixmap generatedIconPixmap(QIcon::Mode iconMode, const QPixmap &pixmap, 01627 const QStyleOption *opt) const; 01628 bool eventFilter(QObject *, QEvent *); 01629 01630 protected Q_SLOTS: 01631 int layoutSpacingImplementation(QSizePolicy::ControlType control1, 01632 QSizePolicy::ControlType control2, Qt::Orientation orientation, 01633 const QStyleOption *option, const QWidget *widget) const; 01634 01635 QIcon standardIconImplementation(StandardPixmap standardIcon, const QStyleOption *option = 0, 01636 const QWidget *widget = 0) const; 01638 private: 01639 KStylePrivate * const d; 01640 }; 01641 01642 template<typename T> 01643 const char* kstyleName() 01644 { return "default"; } 01645 01655 template<typename T> 01656 class KStyleFactory: public QStylePlugin 01657 { 01658 QStringList keys() const 01659 { 01660 QStringList l; 01661 l << kstyleName<T>(); 01662 return l; 01663 } 01664 01665 QStyle* create(const QString& id) 01666 { 01667 QStringList names = keys(); 01668 //check whether included in the keys 01669 if (names.contains(id, Qt::CaseInsensitive)) 01670 return new T(); 01671 01672 return 0; 01673 } 01674 }; 01675 01676 // get the pointed-to type from a pointer 01677 template<typename T> 01678 T KStyle::extractOptionHelper(T*) 01679 { 01680 return T(); 01681 } 01682 01683 template<typename T> 01684 T KStyle::extractOption(Option* option) 01685 { 01686 if (option) { 01687 if (dynamic_cast<T>(option)) 01688 return static_cast<T>(option); 01689 // Ugly hacks for when RTLD_GLOBAL is not used (quite common with plugins, really) 01690 // and dynamic_cast fails. 01691 // This is still partially broken as it doesn't take into account subclasses. 01692 // ### KDE5 do this somehow differently 01693 if ( qstrcmp(typeid(*option).name(), typeid(extractOptionHelper(static_cast<T>(0))).name()) == 0 ) 01694 return static_cast<T>(option); 01695 } 01696 01697 //### warn if cast failed? 01698 01699 //since T is a pointer type, need this to get to the static. 01700 return static_cast<T>(0)->defaultOption(); 01701 } 01702 01703 #define K_EXPORT_STYLE(name,type) template<> const char* kstyleName<type>() { return name; } \ 01704 Q_EXPORT_PLUGIN(KStyleFactory<type>) 01705 01706 #endif 01707 // kate: indent-width 4; replace-tabs on; tab-width 4; space-indent on;
KDE 4.6 API Reference