QElectroTech  0.70
diagramschooser.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2019 The QElectroTech Team
3  This file is part of QElectroTech.
4 
5  QElectroTech is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 2 of the License, or
8  (at your option) any later version.
9 
10  QElectroTech is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "diagramschooser.h"
19 #include "qetproject.h"
20 #include "diagram.h"
21 
27 DiagramsChooser::DiagramsChooser(QETProject *project, QWidget *parent) :
28  QScrollArea(parent),
29  project_(project),
30  widget0_(nullptr),
31  vlayout0_(nullptr)
32 {
33  setFrameShadow(QFrame::Sunken);
34  setFrameShape(QFrame::StyledPanel);
35  setLineWidth(3);
36  setMidLineWidth(3);
37 
38  setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
39  setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
40  setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding));
41 
42  updateList();
43 
44  setWidget(widget0_);
45 }
46 
51 }
52 
57  return(project_);
58 }
59 
63 QList<Diagram *> DiagramsChooser::selectedDiagrams() const {
64  QList<Diagram *> selected_diagrams;
65  foreach(Diagram *diagram, project_ -> diagrams()) {
66  QCheckBox *check_box = diagrams_[diagram];
67  if (check_box && check_box -> isChecked()) {
68  selected_diagrams << diagram;
69  }
70  }
71  return(selected_diagrams);
72 }
73 
77 QList<Diagram *> DiagramsChooser::nonSelectedDiagrams() const {
78  QList<Diagram *> selected_diagrams;
79  foreach(Diagram *diagram, diagrams_.keys()) {
80  if (!(diagrams_[diagram] -> isChecked())) {
81  selected_diagrams << diagram;
82  }
83  }
84  return(selected_diagrams);
85 }
86 
90 bool DiagramsChooser::diagramIsSelected(Diagram *const diagram) const {
91  QCheckBox *checkbox = diagrams_.value(diagram);
92  if (!checkbox) return(false);
93  return(checkbox -> isChecked());
94 }
95 
104 void DiagramsChooser::setSelectedDiagrams(const QList<Diagram *> &diagrams_list, bool select, bool reset) {
105  // evite d'emettre une rafale de signaux pour cette operation
106  blockSignals(true);
107 
108  // deselectionne tous les schemas si demande
109  if (reset) {
110  foreach(QCheckBox *check_box, diagrams_.values()) {
111  check_box -> setChecked(false);
112  }
113  }
114 
115  int changes = 0;
116  QCheckBox *check_box;
117  foreach(Diagram *diagram, diagrams_list) {
118  if ((check_box = diagrams_[diagram])) {
119  if (check_box -> isChecked() != select) {
120  check_box -> setChecked(select);
121  ++ changes;
122  }
123  }
124  }
125 
126  blockSignals(false);
127  if (reset || changes) {
128  emit(selectionChanged());
129  }
130 }
131 
138  blockSignals(true);
139  foreach(QCheckBox *check_box, diagrams_.values()) {
140  check_box -> setChecked(select);
141  }
142  blockSignals(false);
143  emit(selectionChanged());
144 }
145 
150  if (!project_) return;
151 
152  // retient la liste des schemas deja selectionnes
153  QList<Diagram *> selected_diagrams = selectedDiagrams();
154 
155  // detruit les checkbox existantes
156  QList<QCheckBox *> checkboxes = diagrams_.values();
157  qDeleteAll(checkboxes.begin(), checkboxes.end());
158 
159  buildLayout();
160 
161  // recree les checkbox necessaires
162  foreach(Diagram *diagram, project_ -> diagrams()) {
163  // titre du schema
164  QString diagram_title = diagram -> title();
165  if (diagram_title.isEmpty()) diagram_title = tr("Folio sans titre");
166 
167  QCheckBox *checkbox = new QCheckBox(diagram_title);
168  checkbox -> setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum));
169  checkbox -> setChecked(selected_diagrams.contains(diagram));
170  connect(checkbox, SIGNAL(toggled(bool)), this, SIGNAL(selectionChanged()));
171  diagrams_.insert(diagram, checkbox);
172  vlayout0_ -> addWidget(checkbox, 0, Qt::AlignLeft | Qt::AlignTop);
173  }
174 }
175 
180  if (vlayout0_) return;
181  vlayout0_ = new QVBoxLayout();
182  widget0_ = new QWidget();
183  widget0_ -> setLayout(vlayout0_);
184  widget0_ -> setMinimumSize(170, 0);
185  widget0_ -> setMaximumSize(470, 10000);
186  widget0_ -> setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum));
187 }
~DiagramsChooser() override
QList< Diagram * > selectedDiagrams() const
bool diagramIsSelected(Diagram *const) const
DiagramsChooser(QETProject *, QWidget *=nullptr)
QHash< Diagram *, QCheckBox * > diagrams_
QIcon tr
Definition: qeticons.cpp:204
QWidget * widget0_
void selectionChanged()
QETProject * project_
void setSelectedDiagrams(const QList< Diagram *> &, bool=true, bool=true)
void setSelectedAllDiagrams(bool=true)
QVBoxLayout * vlayout0_
QETProject * project() const
QList< Diagram * > nonSelectedDiagrams() const