1 /*
2   controlmodel.cpp
3 
4   This file is part of GammaRay, the Qt application inspection and
5   manipulation tool.
6 
7   Copyright (C) 2012-2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8   Author: Volker Krause <volker.krause@kdab.com>
9 
10   Licensees holding valid commercial KDAB GammaRay licenses may use this file in
11   accordance with GammaRay Commercial License Agreement provided with the Software.
12 
13   Contact info@kdab.com if any conditions of this licensing are not clear to you.
14 
15   This program is free software; you can redistribute it and/or modify
16   it under the terms of the GNU General Public License as published by
17   the Free Software Foundation, either version 2 of the License, or
18   (at your option) any later version.
19 
20   This program is distributed in the hope that it will be useful,
21   but WITHOUT ANY WARRANTY; without even the implied warranty of
22   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23   GNU General Public License for more details.
24 
25   You should have received a copy of the GNU General Public License
26   along with this program.  If not, see <http://www.gnu.org/licenses/>.
27 */
28 
29 #include "controlmodel.h"
30 #include "styleoption.h"
31 #include "styleinspectorinterface.h"
32 #include <core/util.h>
33 
34 #include <QPainter>
35 #include <QStyle>
36 #include <QStyleOption>
37 
38 using namespace GammaRay;
39 
40 struct control_element_t {
41     const char *name;
42     QStyle::ControlElement control;
43     QStyleOption *(*styleOptionFactory)();
44 };
45 
46 #define MAKE_CE(control) { #control, QStyle:: control, &StyleOption::makeStyleOption }
47 #define MAKE_CE_X(control, factory) { #control, QStyle:: control, &StyleOption:: factory }
48 
49 static const control_element_t controlElements[] = {
50     MAKE_CE_X(CE_PushButton, makeButtonStyleOption),
51     MAKE_CE_X(CE_PushButtonBevel, makeButtonStyleOption),
52     MAKE_CE_X(CE_PushButtonLabel, makeButtonStyleOption),
53     MAKE_CE_X(CE_CheckBox, makeButtonStyleOption),
54     MAKE_CE_X(CE_CheckBoxLabel, makeButtonStyleOption),
55     MAKE_CE_X(CE_RadioButton, makeButtonStyleOption),
56     MAKE_CE_X(CE_RadioButtonLabel, makeButtonStyleOption),
57     MAKE_CE_X(CE_TabBarTab, makeTabStyleOption),
58     MAKE_CE_X(CE_TabBarTabShape, makeTabStyleOption),
59     MAKE_CE_X(CE_TabBarTabLabel, makeTabStyleOption),
60     MAKE_CE_X(CE_ProgressBar, makeProgressBarStyleOption),
61     MAKE_CE_X(CE_ProgressBarGroove, makeProgressBarStyleOption),
62     MAKE_CE_X(CE_ProgressBarContents, makeProgressBarStyleOption),
63     MAKE_CE_X(CE_ProgressBarLabel, makeProgressBarStyleOption),
64     MAKE_CE_X(CE_MenuItem, makeMenuStyleOption),
65     MAKE_CE(CE_MenuScroller),
66     MAKE_CE(CE_MenuVMargin),
67     MAKE_CE(CE_MenuHMargin),
68     MAKE_CE(CE_MenuTearoff),
69     MAKE_CE(CE_MenuEmptyArea),
70     MAKE_CE_X(CE_MenuBarItem, makeMenuStyleOption),
71     MAKE_CE(CE_MenuBarEmptyArea),
72     MAKE_CE_X(CE_ToolButtonLabel, makeToolButtonStyleOption),
73     MAKE_CE_X(CE_Header, makeHeaderStyleOption),
74     MAKE_CE_X(CE_HeaderSection, makeHeaderStyleOption),
75     MAKE_CE_X(CE_HeaderLabel, makeHeaderStyleOption),
76     MAKE_CE_X(CE_ToolBoxTab, makeToolBoxStyleOption),
77     MAKE_CE(CE_SizeGrip),
78     MAKE_CE(CE_Splitter),
79     MAKE_CE(CE_RubberBand),
80     MAKE_CE(CE_DockWidgetTitle),
81     MAKE_CE_X(CE_ScrollBarAddLine, makeSliderStyleOption),
82     MAKE_CE_X(CE_ScrollBarSubLine, makeSliderStyleOption),
83     MAKE_CE_X(CE_ScrollBarAddPage, makeSliderStyleOption),
84     MAKE_CE_X(CE_ScrollBarSubPage, makeSliderStyleOption),
85     MAKE_CE_X(CE_ScrollBarSlider, makeSliderStyleOption),
86     MAKE_CE_X(CE_ScrollBarFirst, makeSliderStyleOption),
87     MAKE_CE_X(CE_ScrollBarLast, makeSliderStyleOption),
88     MAKE_CE(CE_FocusFrame),
89     MAKE_CE_X(CE_ComboBoxLabel, makeComboBoxStyleOption),
90     MAKE_CE(CE_ToolBar),
91     MAKE_CE_X(CE_ToolBoxTabShape, makeToolBoxStyleOption),
92     MAKE_CE_X(CE_ToolBoxTabLabel, makeToolBoxStyleOption),
93     MAKE_CE_X(CE_HeaderEmptyArea, makeHeaderStyleOption),
94     MAKE_CE(CE_ColumnViewGrip),
95     MAKE_CE_X(CE_ItemViewItem, makeItemViewStyleOption),
96     MAKE_CE_X(CE_ShapedFrame, makeFrameStyleOption)
97 };
98 
ControlModel(QObject * parent)99 ControlModel::ControlModel(QObject *parent)
100     : AbstractStyleElementStateTable(parent)
101 {
102 }
103 
doData(int row,int column,int role) const104 QVariant ControlModel::doData(int row, int column, int role) const
105 {
106     if (role == Qt::DecorationRole) {
107         QPixmap pixmap(m_interface->cellSizeHint());
108         QPainter painter(&pixmap);
109         Util::drawTransparencyPattern(&painter, pixmap.rect());
110         painter.scale(m_interface->cellZoom(), m_interface->cellZoom());
111 
112         QScopedPointer<QStyleOption> opt(controlElements[row].styleOptionFactory());
113         fillStyleOption(opt.data(), column);
114         m_style->drawControl(controlElements[row].control, opt.data(), &painter);
115         return pixmap;
116     }
117 
118     return AbstractStyleElementStateTable::doData(row, column, role);
119 }
120 
doRowCount() const121 int ControlModel::doRowCount() const
122 {
123     return sizeof(controlElements) / sizeof(controlElements[0]);
124 }
125 
headerData(int section,Qt::Orientation orientation,int role) const126 QVariant ControlModel::headerData(int section, Qt::Orientation orientation, int role) const
127 {
128     if (orientation == Qt::Vertical && role == Qt::DisplayRole)
129         return controlElements[section].name;
130     return AbstractStyleElementStateTable::headerData(section, orientation, role);
131 }
132