Rev 17386 | Rev 17389 | 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 | |||
34 | ScTreeWidgetDelegate::ScTreeWidgetDelegate(QTreeWidget *view, QWidget *parent) : QItemDelegate(parent), m_view(view) |
||
35 | { |
||
36 | } |
||
37 | |||
38 | void ScTreeWidgetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
||
39 | { |
||
40 | const QAbstractItemModel *model = index.model(); |
||
41 | Q_ASSERT(model); |
||
42 | if (!model->parent(index).isValid()) |
||
43 | { |
||
44 | // this is a top-level item. |
||
45 | QStyleOptionButton buttonOption; |
||
46 | buttonOption.state = option.state; |
||
12396 | jghali | 47 | #if defined(Q_WS_MAC) || defined(Q_WS_WIN) |
12295 | fschmid | 48 | buttonOption.state |= QStyle::State_Raised; |
49 | #endif |
||
50 | buttonOption.state &= ~QStyle::State_HasFocus; |
||
51 | buttonOption.rect = option.rect; |
||
52 | buttonOption.palette = option.palette; |
||
53 | m_view->style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter, m_view); |
||
54 | static const int i = 9; // ### hardcoded in qcommonstyle.cpp |
||
55 | QRect r = option.rect; |
||
56 | painter->save(); |
||
17386 | fschmid | 57 | if (option.state & QStyle::State_Enabled) |
58 | painter->setBrush(Qt::black); |
||
59 | else |
||
60 | painter->setBrush(Qt::gray); |
||
12295 | fschmid | 61 | painter->setPen(Qt::NoPen); |
17383 | fschmid | 62 | QRect rect = QRect(r.left()+6, r.top()+6, r.height()-12, r.height()-12); |
12295 | fschmid | 63 | QPolygon pa(3); |
64 | if (m_view->isExpanded(index)) |
||
65 | { |
||
66 | pa.setPoint(0, rect.left(), rect.top()); |
||
67 | pa.setPoint(1, rect.right(), rect.top()); |
||
68 | pa.setPoint(2, rect.center().x(), rect.bottom()); |
||
69 | } |
||
70 | else |
||
71 | { |
||
72 | pa.setPoint(0, rect.left(), rect.top()); |
||
73 | pa.setPoint(1, rect.left(), rect.bottom()); |
||
74 | pa.setPoint(2, rect.right(), rect.center().y()); |
||
75 | } |
||
76 | painter->setRenderHint(QPainter::Antialiasing, true); |
||
77 | painter->drawPolygon(pa); |
||
78 | painter->restore(); |
||
79 | // draw text |
||
80 | QRect textrect = QRect(r.left() + i*2, r.top(), r.width() - ((5*i)/2), r.height()); |
||
17383 | fschmid | 81 | QString text = option.fontMetrics.elidedText(model->data(index, Qt::DisplayRole).toString(), Qt::ElideMiddle, textrect.width(), Qt::TextShowMnemonic); |
82 | m_view->style()->drawItemText(painter, textrect, Qt::AlignCenter | Qt::TextShowMnemonic, option.palette, (option.state & QStyle::State_Enabled), text, QPalette::Text); |
||
12295 | fschmid | 83 | } |
84 | else |
||
85 | QItemDelegate::paint(painter, option, index); |
||
86 | } |
||
87 | |||
88 | QSize ScTreeWidgetDelegate::sizeHint(const QStyleOptionViewItem &opt, const QModelIndex &index) const |
||
89 | { |
||
17383 | fschmid | 90 | QSize sz = QItemDelegate::sizeHint(opt, index) + QSize(2, 6); |
12295 | fschmid | 91 | return sz; |
92 | } |
||
93 | |||
94 | ScTreeWidget::ScTreeWidget(QWidget* pa) : QTreeWidget(pa) |
||
95 | { |
||
12296 | fschmid | 96 | setFocusPolicy(Qt::NoFocus); |
12295 | fschmid | 97 | setColumnCount(1); |
98 | setItemDelegate(new ScTreeWidgetDelegate(this, this)); |
||
99 | setRootIsDecorated(false); |
||
100 | setIndentation(0); |
||
101 | header()->hide(); |
||
12296 | fschmid | 102 | header()->setResizeMode(QHeaderView::Stretch); |
12295 | fschmid | 103 | viewport()->setBackgroundRole(QPalette::Window); |
17383 | fschmid | 104 | m_toolbox_mode = false; |
12308 | fschmid | 105 | connect(this, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(handleMousePress(QTreeWidgetItem*))); |
12295 | fschmid | 106 | } |
107 | |||
17383 | fschmid | 108 | bool ScTreeWidget::event(QEvent *e) |
109 | { |
||
110 | if (e->type() == QEvent::Shortcut) |
||
111 | { |
||
112 | QShortcutEvent *se = static_cast<QShortcutEvent *>(e); |
||
113 | if (se != NULL) |
||
114 | { |
||
115 | int k = se->shortcutId(); |
||
116 | QTreeWidgetItem *item1 = keySList.value(k); |
||
117 | handleMousePress(item1); |
||
118 | return true; |
||
119 | } |
||
120 | } |
||
121 | return QTreeWidget::event(e); |
||
122 | } |
||
123 | |||
12295 | fschmid | 124 | QTreeWidgetItem* ScTreeWidget::addWidget(QString title, QWidget* widget) |
125 | { |
||
126 | QTreeWidgetItem *item1 = new QTreeWidgetItem(this); |
||
127 | item1->setText(0, title); |
||
128 | item1->setFlags(Qt::ItemIsEnabled); |
||
129 | QTreeWidgetItem *item2 = new QTreeWidgetItem(item1); |
||
130 | item2->setFlags(Qt::ItemIsEnabled); |
||
12296 | fschmid | 131 | // hack to work around a bug in Qt-4.3.4 |
132 | widget->layout()->activate(); |
||
133 | widget->setMinimumSize(widget->layout()->minimumSize()); |
||
12298 | fschmid | 134 | item2->setSizeHint(0, widget->layout()->minimumSize()); |
12296 | fschmid | 135 | // end hack |
12295 | fschmid | 136 | setItemWidget(item2, 0, widget); |
17383 | fschmid | 137 | QKeySequence newMnemonic = QKeySequence::mnemonic(title); |
138 | // grabShortcut(newMnemonic); |
||
139 | keySList.insert(grabShortcut(newMnemonic), item1); |
||
12295 | fschmid | 140 | return item1; |
141 | } |
||
142 | |||
17383 | fschmid | 143 | void ScTreeWidget::setToolBoxMode(bool enable) |
144 | { |
||
145 | m_toolbox_mode = enable; |
||
146 | } |
||
147 | |||
148 | int ScTreeWidget::addItem(QWidget* widget, QString title) |
||
149 | { |
||
150 | QTreeWidgetItem *top = addWidget(title, widget); |
||
151 | return indexOfTopLevelItem(top); |
||
152 | } |
||
153 | |||
154 | QWidget* ScTreeWidget::widget(int index) |
||
155 | { |
||
156 | if ((index < 0) || (index >= topLevelItemCount())) |
||
157 | return NULL; |
||
158 | QTreeWidgetItem *top = topLevelItem(index); |
||
159 | if (top->childCount() == 0) |
||
160 | return NULL; |
||
161 | QTreeWidgetItem *child = top->child(0); |
||
162 | return itemWidget(child, 0); |
||
163 | } |
||
164 | |||
165 | void ScTreeWidget::setItemEnabled(int index, bool enable) |
||
166 | { |
||
167 | if ((index < 0) || (index >= topLevelItemCount())) |
||
168 | return; |
||
169 | if (enable) |
||
170 | topLevelItem(index)->setFlags(Qt::ItemIsEnabled); |
||
171 | else |
||
172 | topLevelItem(index)->setFlags(0); |
||
17387 | fschmid | 173 | QTreeWidgetItem *child = topLevelItem(index)->child(0); |
174 | if (child != NULL) |
||
175 | itemWidget(child, 0)->setEnabled(enable); |
||
17383 | fschmid | 176 | } |
177 | |||
178 | bool ScTreeWidget::isItemEnabled(int index) |
||
179 | { |
||
180 | if ((index < 0) || (index >= topLevelItemCount())) |
||
181 | return false; |
||
182 | return !topLevelItem(index)->isDisabled(); |
||
183 | } |
||
184 | |||
185 | void ScTreeWidget::setCurrentIndex(int index) |
||
186 | { |
||
187 | if ((index < 0) || (index >= topLevelItemCount())) |
||
188 | return; |
||
189 | int tops = topLevelItemCount(); |
||
190 | for (int t = 0; t < tops; t++) |
||
191 | { |
||
192 | setItemExpanded(topLevelItem(t), false); |
||
193 | } |
||
194 | QTreeWidgetItem *top = topLevelItem(index); |
||
195 | setCurrentItem(top); |
||
196 | setItemExpanded(top, true); |
||
197 | int wide = 0; |
||
198 | if (top->childCount() != 0) |
||
199 | { |
||
200 | QTreeWidgetItem *child = top->child(0); |
||
201 | if (child != 0) |
||
202 | wide = itemWidget(child, 0)->minimumSizeHint().width()+5; |
||
203 | } |
||
204 | if (wide != 0) |
||
205 | setColumnWidth(0, wide); |
||
206 | else |
||
207 | resizeColumnToContents(0); |
||
208 | } |
||
209 | |||
210 | int ScTreeWidget::currentIndex() |
||
211 | { |
||
212 | int index = -1; |
||
213 | QTreeWidgetItem* item = currentItem(); |
||
214 | if (item->parent() == 0) |
||
215 | index = indexOfTopLevelItem(item); |
||
216 | else |
||
217 | index = indexOfTopLevelItem(item->parent()); |
||
218 | return index; |
||
219 | } |
||
220 | |||
221 | void ScTreeWidget::setItemText(int index, QString text) |
||
222 | { |
||
223 | if ((index < 0) || (index >= topLevelItemCount())) |
||
224 | return; |
||
225 | topLevelItem(index)->setText(0, text); |
||
226 | } |
||
227 | |||
12295 | fschmid | 228 | void ScTreeWidget::handleMousePress(QTreeWidgetItem *item) |
229 | { |
||
230 | if (item == 0) |
||
231 | return; |
||
232 | if (item->parent() == 0) |
||
233 | { |
||
17383 | fschmid | 234 | if (item->isDisabled()) |
235 | { |
||
236 | setItemExpanded(item, false); |
||
237 | return; |
||
238 | } |
||
239 | int wide = 0; |
||
12295 | fschmid | 240 | int tops = topLevelItemCount(); |
17383 | fschmid | 241 | if (m_toolbox_mode) |
12295 | fschmid | 242 | { |
17383 | fschmid | 243 | for (int t = 0; t < tops; t++) |
12295 | fschmid | 244 | { |
17383 | fschmid | 245 | setItemExpanded(topLevelItem(t), false); |
246 | } |
||
247 | setCurrentItem(item); |
||
248 | setItemExpanded(item, true); |
||
249 | if (item->childCount() != 0) |
||
250 | { |
||
251 | QTreeWidgetItem *child = item->child(0); |
||
252 | if (child != 0) |
||
253 | wide = itemWidget(child, 0)->minimumSizeHint().width()+5; |
||
254 | } |
||
255 | if (wide != 0) |
||
256 | setColumnWidth(0, wide); |
||
257 | else |
||
258 | resizeColumnToContents(0); |
||
259 | } |
||
260 | else |
||
261 | { |
||
262 | setItemExpanded(item, !isItemExpanded(item)); |
||
263 | for (int t = 0; t < tops; t++) |
||
264 | { |
||
265 | QTreeWidgetItem *top = topLevelItem(t); |
||
266 | if (isItemExpanded(top)) |
||
12295 | fschmid | 267 | { |
17383 | fschmid | 268 | if (top->childCount() != 0) |
269 | { |
||
270 | QTreeWidgetItem *child = top->child(0); |
||
271 | if (child != 0) |
||
272 | wide = qMax(wide, itemWidget(child, 0)->minimumSizeHint().width()+5); |
||
273 | } |
||
12295 | fschmid | 274 | } |
275 | } |
||
276 | } |
||
277 | if (wide != 0) |
||
278 | setColumnWidth(0, wide); |
||
279 | else |
||
280 | resizeColumnToContents(0); |
||
17383 | fschmid | 281 | emit currentChanged(indexOfTopLevelItem(item)); |
12295 | fschmid | 282 | } |
283 | } |