BiblioteQ: Source/biblioteq_woody.h Source File

BiblioteQ

biblioteq_woody.h
1 /*
2 ** Copyright (c) Alexis Megas.
3 ** All rights reserved.
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions
7 ** are met:
8 ** 1. Redistributions of source code must retain the above copyright
9 ** notice, this list of conditions and the following disclaimer.
10 ** 2. Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
13 ** 3. The name of the author may not be used to endorse or promote products
14 ** derived from Woody without specific prior written permission.
15 **
16 ** WOODY IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 ** WOODY, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #ifndef _woody_collapse_expand_tool_button_h_
29 #define _woody_collapse_expand_tool_button_h_
30
31 #include <QHeaderView>
32 #include <QPointer>
33 #include <QScrollBar>
34 #include <QToolButton>
35 #include <QTreeWidget>
36
37 class woody_collapse_expand_tool_button: public QToolButton
38 {
39 Q_OBJECT
40
41 public:
42 woody_collapse_expand_tool_button(QTreeWidget *tree):QToolButton(tree)
43 {
44 m_tree = tree;
45
46 if(m_tree && m_tree->header())
47 {
48 m_tree->header()->setDefaultAlignment(Qt::AlignCenter);
49 m_tree->header()->setMinimumHeight(30);
50 move(5, (m_tree->header()->size().height() - 25) / 2 + 2);
51 resize(25, 25);
52 setCheckable(true);
53
54 auto font(this->font());
55
56 font.setStyleHint(QFont::Courier);
57 setFont(font);
58 setStyleSheet("QToolButton {border: none;}"
59 "QToolButton::menu-button {border: none;}");
60 setText(tr("+"));
61 setToolTip(tr("Collapse / Expand"));
62 connect(m_tree,
63 SIGNAL(itemCollapsed(QTreeWidgetItem *)),
64 this,
65 SLOT(slot_item_collapsed_expanded(void)));
66 connect(m_tree,
67 SIGNAL(itemExpanded(QTreeWidgetItem *)),
68 this,
69 SLOT(slot_item_collapsed_expanded(void)));
70 connect(m_tree->horizontalScrollBar(),
71 SIGNAL(valueChanged(int)),
72 this,
73 SLOT(slot_scroll(int)));
74 connect(this,
75 SIGNAL(clicked(void)),
76 this,
77 SLOT(slot_collapse(void)));
78 }
79 else
80 setVisible(false);
81 }
82
83 private:
84 QPointer<QTreeWidget> m_tree;
85
86 private slots:
87 void slot_collapse(void)
88 {
89 if(m_tree)
90 {
91 disconnect(m_tree,
92 SIGNAL(itemCollapsed(QTreeWidgetItem *)),
93 this,
94 SLOT(slot_item_collapsed_expanded(void)));
95 disconnect(m_tree,
96 SIGNAL(itemExpanded(QTreeWidgetItem *)),
97 this,
98 SLOT(slot_item_collapsed_expanded(void)));
99 }
100
101 if(isChecked())
102 {
103 if(m_tree)
104 m_tree->expandAll();
105
106 setText(tr("-"));
107 }
108 else
109 {
110 if(m_tree)
111 m_tree->collapseAll();
112
113 setText(tr("+"));
114 }
115
116 if(m_tree)
117 {
118 connect(m_tree,
119 SIGNAL(itemCollapsed(QTreeWidgetItem *)),
120 this,
121 SLOT(slot_item_collapsed_expanded(void)));
122 connect(m_tree,
123 SIGNAL(itemExpanded(QTreeWidgetItem *)),
124 this,
125 SLOT(slot_item_collapsed_expanded(void)));
126 }
127 }
128
129 void slot_item_collapsed_expanded(void)
130 {
131 if(!m_tree)
132 return;
133
134 int expanded = 0;
135
136 for(int i = 0; i < m_tree->topLevelItemCount(); i++)
137 if(m_tree->topLevelItem(i) && m_tree->topLevelItem(i)->childCount() > 0)
138 expanded += m_tree->topLevelItem(i)->isExpanded() ? 1 : 0;
139
140 if(expanded == 0)
141 {
142 setChecked(false);
143 setText(tr("+"));
144 }
145 else
146 {
147 setChecked(true);
148 setText(tr("-"));
149 }
150 }
151
152 void slot_scroll(int value)
153 {
154 setVisible(geometry().right() - geometry().width() / 2 > value);
155 }
156 };
157
158 #endif
woody_collapse_expand_tool_button
Definition: biblioteq_woody.h:38