Rev 22521 | Rev 22635 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
12295 | fschmid | 1 | /* |
2 | For general Scribus (>=1.3.2) copyright and licensing information please refer |
||
3 | to the COPYING file provided with the program. Following this notice may exist |
||
4 | a copyright and/or license notice that predates the release of Scribus 1.3.2 |
||
5 | for which a new license (GPL+exception) is in place. |
||
6 | */ |
||
7 | /*************************************************************************** |
||
8 | sctreewidget.cpp - description |
||
9 | ------------------- |
||
10 | begin : Wed Jun 18 2008 |
||
11 | copyright : (C) 2008 by Franz Schmid |
||
12 | email : Franz.Schmid@altmuehlnet.de |
||
13 | ***************************************************************************/ |
||
14 | |||
15 | /*************************************************************************** |
||
16 | * * |
||
17 | * This program is free software; you can redistribute it and/or modify * |
||
18 | * it under the terms of the GNU General Public License as published by * |
||
19 | * the Free Software Foundation; either version 2 of the License, or * |
||
20 | * (at your option) any later version. * |
||
21 | * * |
||
22 | ***************************************************************************/ |
||
23 | |||
24 | #include <QAbstractItemModel> |
||
25 | #include <QStyle> |
||
26 | #include <QPainter> |
||
27 | #include <QHeaderView> |
||
12296 | fschmid | 28 | #include <QLayout> |
17383 | fschmid | 29 | #include <QShortcutEvent> |
12295 | fschmid | 30 | |
13650 | cbradney | 31 | #include "sctreewidget.h" |
12295 | fschmid | 32 | |
33 | ScTreeWidgetDelegate::ScTreeWidgetDelegate(QTreeWidget *view, QWidget *parent) : QItemDelegate(parent), m_view(view) |
||
34 | { |
||
35 | } |
||
36 | |||
37 | void ScTreeWidgetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
||
38 | { |
||
39 | const QAbstractItemModel *model = index.model(); |
||
40 | Q_ASSERT(model); |
||
41 | if (!model->parent(index).isValid()) |
||
42 | { |
||
43 | // this is a top-level item. |
||
44 | QStyleOptionButton buttonOption; |
||
45 | buttonOption.state = option.state; |
||
18204 | fschmid | 46 | #if defined(Q_OS_MAC) || defined(Q_OS_WIN) |
12295 | fschmid | 47 | buttonOption.state |= QStyle::State_Raised; |
48 | #endif |
||
49 | buttonOption.state &= ~QStyle::State_HasFocus; |
||
50 | buttonOption.rect = option.rect; |
||
51 | buttonOption.palette = option.palette; |
||
52 | m_view->style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter, m_view); |
||
53 | static const int i = 9; // ### hardcoded in qcommonstyle.cpp |
||
54 | QRect r = option.rect; |
||
55 | painter->save(); |
||
17386 | fschmid | 56 | if (option.state & QStyle::State_Enabled) |
57 | painter->setBrush(Qt::black); |
||
58 | else |
||
59 | painter->setBrush(Qt::gray); |
||
12295 | fschmid | 60 | painter->setPen(Qt::NoPen); |
17383 | fschmid | 61 | QRect rect = QRect(r.left()+6, r.top()+6, r.height()-12, r.height()-12); |
12295 | fschmid | 62 | QPolygon pa(3); |
63 | if (m_view->isExpanded(index)) |
||
64 | { |
||
65 | pa.setPoint(0, rect.left(), rect.top()); |
||
66 | pa.setPoint(1, rect.right(), rect.top()); |
||
67 | pa.setPoint(2, rect.center().x(), rect.bottom()); |
||
68 | } |
||
69 | else |
||
70 | { |
||
71 | pa.setPoint(0, rect.left(), rect.top()); |
||
72 | pa.setPoint(1, rect.left(), rect.bottom()); |
||
73 | pa.setPoint(2, rect.right(), rect.center().y()); |
||
74 | } |
||
75 | painter->setRenderHint(QPainter::Antialiasing, true); |
||
76 | painter->drawPolygon(pa); |
||
77 | painter->restore(); |
||
78 | // draw text |
||
79 | QRect textrect = QRect(r.left() + i*2, r.top(), r.width() - ((5*i)/2), r.height()); |
||
17383 | fschmid | 80 | QString text = option.fontMetrics.elidedText(model->data(index, Qt::DisplayRole).toString(), Qt::ElideMiddle, textrect.width(), Qt::TextShowMnemonic); |
81 | m_view->style()->drawItemText(painter, textrect, Qt::AlignCenter | Qt::TextShowMnemonic, option.palette, (option.state & QStyle::State_Enabled), text, QPalette::Text); |
||
12295 | fschmid | 82 | } |
83 | else |
||
84 | QItemDelegate::paint(painter, option, index); |
||
85 | } |
||
86 | |||
87 | QSize ScTreeWidgetDelegate::sizeHint(const QStyleOptionViewItem &opt, const QModelIndex &index) const |
||
88 | { |
||
17383 | fschmid | 89 | QSize sz = QItemDelegate::sizeHint(opt, index) + QSize(2, 6); |
12295 | fschmid | 90 | return sz; |
91 | } |
||
92 | |||
93 | ScTreeWidget::ScTreeWidget(QWidget* pa) : QTreeWidget(pa) |
||
94 | { |
||
21095 | jghali | 95 | // #12204: Opening/Closing Properties palette tabs moves the view to the first document |
96 | // When a tab looses focus, this cause problems has Qt has to fallback to another widget |
||
97 | // for setting new focus, and this may be an arbitrary document window |
||
98 | // setFocusPolicy(Qt::NoFocus); |
||
12295 | fschmid | 99 | setColumnCount(1); |
100 | setItemDelegate(new ScTreeWidgetDelegate(this, this)); |
||
101 | setRootIsDecorated(false); |
||
102 | setIndentation(0); |
||
103 | header()->hide(); |
||
18194 | fschmid | 104 | header()->setSectionResizeMode(QHeaderView::Stretch); |
12295 | fschmid | 105 | viewport()->setBackgroundRole(QPalette::Window); |
17389 | fschmid | 106 | setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); |
17383 | fschmid | 107 | m_toolbox_mode = false; |
12308 | fschmid | 108 | connect(this, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(handleMousePress(QTreeWidgetItem*))); |
12295 | fschmid | 109 | } |
110 | |||
17383 | fschmid | 111 | bool ScTreeWidget::event(QEvent *e) |
112 | { |
||
113 | if (e->type() == QEvent::Shortcut) |
||
114 | { |
||
115 | QShortcutEvent *se = static_cast<QShortcutEvent *>(e); |
||
22521 | craig | 116 | if (se != nullptr) |
17383 | fschmid | 117 | { |
118 | int k = se->shortcutId(); |
||
119 | QTreeWidgetItem *item1 = keySList.value(k); |
||
120 | handleMousePress(item1); |
||
121 | return true; |
||
122 | } |
||
123 | } |
||
124 | return QTreeWidget::event(e); |
||
125 | } |
||
126 | |||
12295 | fschmid | 127 | QTreeWidgetItem* ScTreeWidget::addWidget(QString title, QWidget* widget) |
128 | { |
||
129 | QTreeWidgetItem *item1 = new QTreeWidgetItem(this); |
||
130 | item1->setText(0, title); |
||
131 | item1->setFlags(Qt::ItemIsEnabled); |
||
132 | QTreeWidgetItem *item2 = new QTreeWidgetItem(item1); |
||
133 | item2->setFlags(Qt::ItemIsEnabled); |
||
12296 | fschmid | 134 | // hack to work around a bug in Qt-4.3.4 |
21575 | craig | 135 | // if (widget->layout()) |
136 | // { |
||
137 | // widget->layout()->activate(); |
||
138 | // widget->setMinimumSize(widget->layout()->minimumSize()); |
||
139 | // item2->setSizeHint(0, widget->layout()->minimumSize()); |
||
140 | // } |
||
12296 | fschmid | 141 | // end hack |
12295 | fschmid | 142 | setItemWidget(item2, 0, widget); |
17383 | fschmid | 143 | QKeySequence newMnemonic = QKeySequence::mnemonic(title); |
144 | // grabShortcut(newMnemonic); |
||
145 | keySList.insert(grabShortcut(newMnemonic), item1); |
||
12295 | fschmid | 146 | return item1; |
147 | } |
||
148 | |||
17383 | fschmid | 149 | void ScTreeWidget::setToolBoxMode(bool enable) |
150 | { |
||
151 | m_toolbox_mode = enable; |
||
152 | } |
||
153 | |||
154 | int ScTreeWidget::addItem(QWidget* widget, QString title) |
||
155 | { |
||
156 | QTreeWidgetItem *top = addWidget(title, widget); |
||
157 | return indexOfTopLevelItem(top); |
||
158 | } |
||
159 | |||
160 | QWidget* ScTreeWidget::widget(int index) |
||
161 | { |
||
162 | if ((index < 0) || (index >= topLevelItemCount())) |
||
22521 | craig | 163 | return nullptr; |
17383 | fschmid | 164 | QTreeWidgetItem *top = topLevelItem(index); |
165 | if (top->childCount() == 0) |
||
22521 | craig | 166 | return nullptr; |
17383 | fschmid | 167 | QTreeWidgetItem *child = top->child(0); |
168 | return itemWidget(child, 0); |
||
169 | } |
||
170 | |||
171 | void ScTreeWidget::setItemEnabled(int index, bool enable) |
||
172 | { |
||
173 | if ((index < 0) || (index >= topLevelItemCount())) |
||
174 | return; |
||
175 | if (enable) |
||
176 | topLevelItem(index)->setFlags(Qt::ItemIsEnabled); |
||
177 | else |
||
178 | topLevelItem(index)->setFlags(0); |
||
17387 | fschmid | 179 | QTreeWidgetItem *child = topLevelItem(index)->child(0); |
22521 | craig | 180 | if (child != nullptr) |
17387 | fschmid | 181 | itemWidget(child, 0)->setEnabled(enable); |
17383 | fschmid | 182 | } |
183 | |||
184 | bool ScTreeWidget::isItemEnabled(int index) |
||
185 | { |
||
186 | if ((index < 0) || (index >= topLevelItemCount())) |
||
187 | return false; |
||
188 | return !topLevelItem(index)->isDisabled(); |
||
189 | } |
||
190 | |||
191 | void ScTreeWidget::setCurrentIndex(int index) |
||
192 | { |
||
193 | if ((index < 0) || (index >= topLevelItemCount())) |
||
194 | return; |
||
195 | int tops = topLevelItemCount(); |
||
196 | for (int t = 0; t < tops; t++) |
||
197 | { |
||
21093 | jghali | 198 | topLevelItem(t)->setExpanded(false); |
17383 | fschmid | 199 | } |
200 | QTreeWidgetItem *top = topLevelItem(index); |
||
201 | setCurrentItem(top); |
||
21093 | jghali | 202 | top->setExpanded(true); |
17383 | fschmid | 203 | int wide = 0; |
204 | if (top->childCount() != 0) |
||
205 | { |
||
206 | QTreeWidgetItem *child = top->child(0); |
||
22601 | craig | 207 | if (child != nullptr) |
17383 | fschmid | 208 | wide = itemWidget(child, 0)->minimumSizeHint().width()+5; |
209 | } |
||
210 | if (wide != 0) |
||
211 | setColumnWidth(0, wide); |
||
212 | else |
||
213 | resizeColumnToContents(0); |
||
214 | } |
||
215 | |||
216 | int ScTreeWidget::currentIndex() |
||
217 | { |
||
218 | int index = -1; |
||
219 | QTreeWidgetItem* item = currentItem(); |
||
22601 | craig | 220 | if (item->parent() == nullptr) |
17383 | fschmid | 221 | index = indexOfTopLevelItem(item); |
222 | else |
||
223 | index = indexOfTopLevelItem(item->parent()); |
||
224 | return index; |
||
225 | } |
||
226 | |||
22601 | craig | 227 | void ScTreeWidget::setItemText(int index, const QString& text) |
17383 | fschmid | 228 | { |
229 | if ((index < 0) || (index >= topLevelItemCount())) |
||
230 | return; |
||
231 | topLevelItem(index)->setText(0, text); |
||
232 | } |
||
233 | |||
12295 | fschmid | 234 | void ScTreeWidget::handleMousePress(QTreeWidgetItem *item) |
235 | { |
||
22601 | craig | 236 | if (item == nullptr) |
12295 | fschmid | 237 | return; |
22601 | craig | 238 | if (item->parent() == nullptr) |
12295 | fschmid | 239 | { |
17383 | fschmid | 240 | if (item->isDisabled()) |
241 | { |
||
21093 | jghali | 242 | item->setExpanded(false); |
17383 | fschmid | 243 | return; |
244 | } |
||
245 | int wide = 0; |
||
12295 | fschmid | 246 | int tops = topLevelItemCount(); |
17383 | fschmid | 247 | if (m_toolbox_mode) |
12295 | fschmid | 248 | { |
17383 | fschmid | 249 | for (int t = 0; t < tops; t++) |
12295 | fschmid | 250 | { |
21093 | jghali | 251 | topLevelItem(t)->setExpanded(false); |
17383 | fschmid | 252 | } |
253 | setCurrentItem(item); |
||
21093 | jghali | 254 | item->setExpanded(true); |
17383 | fschmid | 255 | if (item->childCount() != 0) |
256 | { |
||
257 | QTreeWidgetItem *child = item->child(0); |
||
22601 | craig | 258 | if (child != nullptr) |
17383 | fschmid | 259 | wide = itemWidget(child, 0)->minimumSizeHint().width()+5; |
260 | } |
||
261 | if (wide != 0) |
||
262 | setColumnWidth(0, wide); |
||
263 | else |
||
264 | resizeColumnToContents(0); |
||
265 | } |
||
266 | else |
||
267 | { |
||
21093 | jghali | 268 | item->setExpanded(!item->isExpanded()); |
17383 | fschmid | 269 | for (int t = 0; t < tops; t++) |
270 | { |
||
271 | QTreeWidgetItem *top = topLevelItem(t); |
||
21093 | jghali | 272 | if (top->isExpanded()) |
12295 | fschmid | 273 | { |
17383 | fschmid | 274 | if (top->childCount() != 0) |
275 | { |
||
276 | QTreeWidgetItem *child = top->child(0); |
||
22601 | craig | 277 | if (child != nullptr) |
17383 | fschmid | 278 | wide = qMax(wide, itemWidget(child, 0)->minimumSizeHint().width()+5); |
279 | } |
||
12295 | fschmid | 280 | } |
281 | } |
||
282 | } |
||
283 | if (wide != 0) |
||
284 | setColumnWidth(0, wide); |
||
285 | else |
||
286 | resizeColumnToContents(0); |
||
17936 | craig | 287 | emit currentChanged2(indexOfTopLevelItem(item)); |
12295 | fschmid | 288 | } |
289 | } |