LeechCraft  0.6.70-13729-g7046a9d2a7
Modular cross-platform feature rich live environment.
categoryselector.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "categoryselector.h"
31 #include <algorithm>
32 #include <QStringList>
33 #include <QCheckBox>
34 #include <QVariant>
35 #include <QVBoxLayout>
36 #include <QMoveEvent>
37 #include <QApplication>
38 #include <QDesktopWidget>
39 #include <QAction>
40 #include <QtDebug>
41 #include "ui_categoryselector.h"
42 
43 using namespace LC::Util;
44 const int RoleTag = 52;
45 
47 : QDialog (parent)
48 , Ui_ (new Ui::CategorySelector)
49 , Separator_ ("; ")
50 {
51  setWindowTitle (tr ("Tags selector"));
52  setWindowFlags (Qt::Dialog | Qt::WindowStaysOnTopHint);
53 
54  Ui_->setupUi (this);
55 
56  Ui_->Tree_->setRootIsDecorated (false);
57  Ui_->Tree_->setUniformRowHeights (true);
58 
59  QRect avail = QApplication::desktop ()->availableGeometry (this);
60  setMinimumHeight (avail.height () / 3 * 2);
61 
62  connect (Ui_->Tree_,
63  SIGNAL (itemChanged (QTreeWidgetItem*, int)),
64  this,
65  SLOT (buttonToggled ()));
66 
67  QAction *all = new QAction (tr ("Select all"), this);
68  connect (all,
69  SIGNAL (triggered ()),
70  this,
71  SLOT (selectAll ()));
72 
73  QAction *none = new QAction (tr ("Select none"), this);
74  connect (none,
75  SIGNAL (triggered ()),
76  this,
77  SLOT (selectNone ()));
78 
79  Ui_->Tree_->addAction (all);
80  Ui_->Tree_->addAction (none);
81 
82  Ui_->Tree_->setContextMenuPolicy (Qt::ActionsContextMenu);
83 
85 }
86 
87 void CategorySelector::SetCaption (const QString& caption)
88 {
89  Ui_->Tree_->setHeaderLabel (caption);
90  Caption_ = caption;
91 }
92 
93 void CategorySelector::setPossibleSelections (QStringList mytags, bool sort)
94 {
95  disconnect (Ui_->Tree_,
96  SIGNAL (itemChanged (QTreeWidgetItem*, int)),
97  this,
98  SLOT (buttonToggled ()));
99 
100  Ui_->Tree_->clear ();
101 
102  if (sort)
103  mytags.sort ();
104 
106  for (const auto& tag : mytags)
107  {
108  if (tag.isEmpty ())
109  continue;
110 
111  auto item = new QTreeWidgetItem ({ tag });
112  item->setCheckState (0, Qt::Unchecked);
113  item->setData (0, RoleTag, tag);
114  items << item;
115  }
116  Ui_->Tree_->addTopLevelItems (items);
117 
118  Ui_->Tree_->setHeaderLabel (Caption_);
119 
120  connect (Ui_->Tree_,
121  SIGNAL (itemChanged (QTreeWidgetItem*, int)),
122  this,
123  SLOT (buttonToggled ()));
124 
125  emit tagsSelectionChanged (QStringList ());
126 }
127 
129 {
130  QStringList tags;
131 
132  for (int i = 0, size = Ui_->Tree_->topLevelItemCount (); i < size; ++i)
133  {
134  const auto item = Ui_->Tree_->topLevelItem (i);
135  if (item->checkState (0) == Qt::Checked)
136  tags += item->data (0, RoleTag).toString ();
137  }
138 
139  return tags;
140 }
141 
143 {
144  QList<int> result;
145 
146  for (int i = 0, size = Ui_->Tree_->topLevelItemCount (); i < size; ++i)
147  {
148  const auto item = Ui_->Tree_->topLevelItem (i);
149  if (item->checkState (0) == Qt::Checked)
150  result << i;
151  }
152 
153  return result;
154 }
155 
156 void CategorySelector::SetSelections (const QStringList& tags)
157 {
158  blockSignals (true);
159  for (int i = 0; i < Ui_->Tree_->topLevelItemCount (); ++i)
160  {
161  const auto& tagVar = Ui_->Tree_->topLevelItem (i)->data (0, RoleTag);
162  const auto state = tags.contains (tagVar.toString ()) ?
163  Qt::Checked :
164  Qt::Unchecked;
165  Ui_->Tree_->topLevelItem (i)->setCheckState (0, state);
166  }
167  blockSignals (false);
168 }
169 
171 {
172  return Separator_;
173 }
174 
175 void CategorySelector::SetSeparator (const QString& sep)
176 {
177  Separator_ = sep;
178 }
179 
181 {
182  switch (mode)
183  {
185  Ui_->ButtonsBox_->setVisible (false);
186  break;
187  case ButtonsMode::Close:
188  Ui_->ButtonsBox_->setStandardButtons (QDialogButtonBox::Close);
189  Ui_->ButtonsBox_->setVisible (true);
190  break;
192  Ui_->ButtonsBox_->setStandardButtons (QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
193  Ui_->ButtonsBox_->setVisible (true);
194  break;
195  }
196 }
197 
198 void CategorySelector::moveEvent (QMoveEvent *e)
199 {
200  QWidget::moveEvent (e);
201  QPoint pos = e->pos ();
202  QRect avail = QApplication::desktop ()->availableGeometry (this);
203  int dx = 0, dy = 0;
204  if (pos.x () + width () > avail.width ())
205  dx = width () + pos.x () - avail.width ();
206  if (pos.y () + height () > avail.height () &&
207  height () < avail.height ())
208  dy = height () + pos.y () - avail.height ();
209 
210  if (dx || dy)
211  move (pos - QPoint (dx, dy));
212 }
213 
215 {
216  disconnect (Ui_->Tree_,
217  SIGNAL (itemChanged (QTreeWidgetItem*, int)),
218  this,
219  SLOT (buttonToggled ()));
220 
221  QStringList tags;
222 
223  for (int i = 0, size = Ui_->Tree_->topLevelItemCount (); i < size; ++i)
224  {
225  const auto item = Ui_->Tree_->topLevelItem (i);
226  item->setCheckState (0, Qt::Checked);
227  tags += item->data (0, RoleTag).toString ();
228  }
229 
230  connect (Ui_->Tree_,
231  SIGNAL (itemChanged (QTreeWidgetItem*, int)),
232  this,
233  SLOT (buttonToggled ()));
234 
235  emit tagsSelectionChanged (tags);
236 }
237 
239 {
240  disconnect (Ui_->Tree_,
241  SIGNAL (itemChanged (QTreeWidgetItem*, int)),
242  this,
243  SLOT (buttonToggled ()));
244 
245  for (int i = 0; i < Ui_->Tree_->topLevelItemCount (); ++i)
246  Ui_->Tree_->topLevelItem (i)->setCheckState (0, Qt::Unchecked);
247 
248  connect (Ui_->Tree_,
249  SIGNAL (itemChanged (QTreeWidgetItem*, int)),
250  this,
251  SLOT (buttonToggled ()));
252 
253  emit tagsSelectionChanged (QStringList ());
254 }
255 
256 void CategorySelector::lineTextChanged (const QString& text)
257 {
258  const auto& tags = text.split (Separator_, QString::SkipEmptyParts);
259  SetSelections (tags);
260 }
261 
262 void CategorySelector::buttonToggled ()
263 {
265 }
266 
void tagsSelectionChanged(const QStringList &newSelections)
Indicates that selections have changed.
void setPossibleSelections(QStringList selections, bool sort=true)
Sets possible selections.
void selectAll()
Selects all variants.
QStringList GetSelections() const
Gets selected items.
void SetCaption(const QString &caption)
Sets the caption of this selector.
void SetSelections(const QStringList &subset)
Selects some of the items.
void selectNone()
Deselects all variants.
CategorySelector(QWidget *parent=0)
Constructor.
QString GetSeparator() const
Returns the separator for the tags.
QList< int > GetSelectedIndexes() const
Gets the indexes of the selected items.
void lineTextChanged(const QString &newText)
Notifies CategorySelector about logical selection changes.
void SetButtonsMode(ButtonsMode)
Sets the buttons mode.
constexpr detail::SelectWhole all
Definition: oral.h:960
virtual void moveEvent(QMoveEvent *)
Checks whether after the move event the selector won&#39;t be beoynd the screen. if it would...
The CategorySelector widget provides a way to select amongst a group of items.
void SetSeparator(const QString &)
Sets the separator for the tags.
const int RoleTag