Rev 12298 | Rev 12396 | 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> |
12295 | fschmid | 29 | |
30 | #include <sctreewidget.h> |
||
31 | |||
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; |
||
46 | #ifdef Q_WS_MAC |
||
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 | buttonOption.features = QStyleOptionButton::None; |
||
53 | m_view->style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter, m_view); |
||
54 | QStyleOption branchOption; |
||
55 | static const int i = 9; // ### hardcoded in qcommonstyle.cpp |
||
56 | QRect r = option.rect; |
||
57 | painter->save(); |
||
58 | painter->setBrush(Qt::black); |
||
59 | painter->setPen(Qt::NoPen); |
||
60 | QRect rect = QRect(r.left()+5, r.top()+5, r.height()-10, r.height()-10); |
||
61 | QPolygon pa(3); |
||
62 | if (m_view->isExpanded(index)) |
||
63 | { |
||
64 | pa.setPoint(0, rect.left(), rect.top()); |
||
65 | pa.setPoint(1, rect.right(), rect.top()); |
||
66 | pa.setPoint(2, rect.center().x(), rect.bottom()); |
||
67 | } |
||
68 | else |
||
69 | { |
||
70 | pa.setPoint(0, rect.left(), rect.top()); |
||
71 | pa.setPoint(1, rect.left(), rect.bottom()); |
||
72 | pa.setPoint(2, rect.right(), rect.center().y()); |
||
73 | } |
||
74 | painter->setRenderHint(QPainter::Antialiasing, true); |
||
75 | painter->drawPolygon(pa); |
||
76 | painter->restore(); |
||
77 | // draw text |
||
78 | QRect textrect = QRect(r.left() + i*2, r.top(), r.width() - ((5*i)/2), r.height()); |
||
79 | QString text = elidedText(option.fontMetrics, textrect.width(), Qt::ElideMiddle, model->data(index, Qt::DisplayRole).toString()); |
||
80 | m_view->style()->drawItemText(painter, textrect, Qt::AlignCenter, option.palette, m_view->isEnabled(), text); |
||
81 | } |
||
82 | else |
||
83 | QItemDelegate::paint(painter, option, index); |
||
84 | } |
||
85 | |||
86 | QSize ScTreeWidgetDelegate::sizeHint(const QStyleOptionViewItem &opt, const QModelIndex &index) const |
||
87 | { |
||
88 | QStyleOptionViewItem option = opt; |
||
89 | QSize sz = QItemDelegate::sizeHint(opt, index) + QSize(2, 2); |
||
90 | return sz; |
||
91 | } |
||
92 | |||
93 | ScTreeWidget::ScTreeWidget(QWidget* pa) : QTreeWidget(pa) |
||
94 | { |
||
12296 | fschmid | 95 | setFocusPolicy(Qt::NoFocus); |
12295 | fschmid | 96 | setColumnCount(1); |
97 | setItemDelegate(new ScTreeWidgetDelegate(this, this)); |
||
98 | setRootIsDecorated(false); |
||
99 | setIndentation(0); |
||
100 | header()->hide(); |
||
12296 | fschmid | 101 | header()->setResizeMode(QHeaderView::Stretch); |
12295 | fschmid | 102 | viewport()->setBackgroundRole(QPalette::Window); |
12308 | fschmid | 103 | connect(this, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(handleMousePress(QTreeWidgetItem*))); |
12295 | fschmid | 104 | } |
105 | |||
106 | QTreeWidgetItem* ScTreeWidget::addWidget(QString title, QWidget* widget) |
||
107 | { |
||
108 | QTreeWidgetItem *item1 = new QTreeWidgetItem(this); |
||
109 | item1->setText(0, title); |
||
110 | item1->setFlags(Qt::ItemIsEnabled); |
||
111 | QTreeWidgetItem *item2 = new QTreeWidgetItem(item1); |
||
112 | item2->setFlags(Qt::ItemIsEnabled); |
||
12296 | fschmid | 113 | // hack to work around a bug in Qt-4.3.4 |
114 | widget->layout()->activate(); |
||
115 | widget->setMinimumSize(widget->layout()->minimumSize()); |
||
12298 | fschmid | 116 | item2->setSizeHint(0, widget->layout()->minimumSize()); |
12296 | fschmid | 117 | // end hack |
12295 | fschmid | 118 | setItemWidget(item2, 0, widget); |
119 | return item1; |
||
120 | } |
||
121 | |||
122 | void ScTreeWidget::handleMousePress(QTreeWidgetItem *item) |
||
123 | { |
||
124 | if (item == 0) |
||
125 | return; |
||
126 | if (item->parent() == 0) |
||
127 | { |
||
128 | setItemExpanded(item, !isItemExpanded(item)); |
||
129 | int tops = topLevelItemCount(); |
||
130 | int wide = 0; |
||
131 | for (int t = 0; t < tops; t++) |
||
132 | { |
||
133 | QTreeWidgetItem *top = topLevelItem(t); |
||
134 | if (isItemExpanded(top)) |
||
135 | { |
||
136 | if (top->childCount() != 0) |
||
137 | { |
||
138 | QTreeWidgetItem *child = top->child(0); |
||
139 | if (child != 0) |
||
140 | wide = qMax(wide, itemWidget(child, 0)->minimumSizeHint().width()+5); |
||
141 | } |
||
142 | } |
||
143 | } |
||
144 | if (wide != 0) |
||
145 | setColumnWidth(0, wide); |
||
146 | else |
||
147 | resizeColumnToContents(0); |
||
148 | return; |
||
149 | } |
||
150 | } |