1 #include "CmdMediator.h"
2 #include "CmdSettingsCurveAddRemove.h"
3 #include "CurveNameList.h"
4 #include "DlgSettingsCurveAddRemove.h"
5 #include "EngaugeAssert.h"
7 #include "MainWindow.h"
12 #include <QMessageBox>
13 #include <QPushButton>
14 #include "QtToString.h"
18 "DlgSettingsCurveAddRemove",
21 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::DlgSettingsCurveAddRemove";
27 DlgSettingsCurveAddRemove::~DlgSettingsCurveAddRemove()
29 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::~DlgSettingsCurveAddRemove";
32 void DlgSettingsCurveAddRemove::appendCurveName (
const QString &curveNameNew,
33 const QString &curveNameOriginal,
36 ENGAUGE_CHECK_PTR (m_curveNameList);
38 int row = m_curveNameList->
rowCount ();
45 void DlgSettingsCurveAddRemove::createButtons (QGridLayout *layout,
48 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::createButtons";
50 m_btnNew =
new QPushButton (
"New...");
51 m_btnNew->setWhatsThis (tr (
"Adds a new curve to the curve list. The curve name can be edited in the curve name list.\n\n"
52 "If a curve is selected then the new curve will be inserted just before it, otherwise the new curve "
53 "will be added at the end.\n\n"
54 "Every curve name must be unique"));
55 m_btnNew->setSizePolicy (QSizePolicy::Fixed, QSizePolicy::Fixed);
56 connect (m_btnNew, SIGNAL (released ()),
this, SLOT (slotNew()));
57 layout->addWidget (m_btnNew, row, 1, 1, 1, Qt::AlignLeft);
59 m_btnRemove =
new QPushButton (
"Remove");
60 m_btnRemove->setWhatsThis (tr (
"Removes the currently selected curve from the curve list.\n\n"
61 "There must always be at least one curve"));
62 m_btnRemove->setSizePolicy (QSizePolicy::Fixed, QSizePolicy::Fixed);
63 connect (m_btnRemove, SIGNAL (released ()),
this, SLOT (slotRemove()));
64 layout->addWidget (m_btnRemove, row++, 2, 1, 1, Qt::AlignRight);
67 void DlgSettingsCurveAddRemove::createListCurves (QGridLayout *layout,
70 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::createListCurves";
72 QLabel *label =
new QLabel (tr (
"Curve Names:"));
73 layout->addWidget (label, row++, 1);
78 m_listCurves =
new QListView;
79 m_listCurves->setWhatsThis (tr (
"List of the curves belonging to this document.\n\n"
80 "Click on a curve name to edit it.\n\n"
81 "Reorder curves by dragging them around."));
82 m_listCurves->setMinimumHeight (200);
83 m_listCurves->setSelectionMode (QAbstractItemView::ExtendedSelection);
84 m_listCurves->setDefaultDropAction (Qt::MoveAction);
85 m_listCurves->setDragDropOverwriteMode (
true);
86 m_listCurves->setDragEnabled (
true);
87 m_listCurves->setDropIndicatorShown (
true);
88 m_listCurves->setDragDropMode (QAbstractItemView::InternalMove);
89 m_listCurves->setViewMode (QListView::ListMode);
90 m_listCurves->setMovement (QListView::Snap);
91 m_listCurves->setModel (m_curveNameList);
92 layout->addWidget (m_listCurves, row++, 1, 1, 2);
93 connect (m_curveNameList, SIGNAL (dataChanged (
const QModelIndex &,
const QModelIndex &,
const QVector<int> &)),
94 this, SLOT (slotDataChanged (
const QModelIndex &,
const QModelIndex &,
const QVector<int> &)));
95 connect (m_listCurves->selectionModel (), SIGNAL (selectionChanged (QItemSelection, QItemSelection)),
96 this, SLOT (slotSelectionChanged (QItemSelection, QItemSelection)));
101 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::createSubPanel";
103 const int EMPTY_COLUMN_WIDTH = 30;
104 const int EMPTY_ROW_HEIGHT = 40;
106 QWidget *subPanel =
new QWidget ();
107 QGridLayout *layout =
new QGridLayout (subPanel);
108 subPanel->setLayout (layout);
111 createListCurves (layout, row);
112 createButtons (layout, row);
114 layout->setColumnStretch (0, 0);
115 layout->setColumnMinimumWidth (0, EMPTY_COLUMN_WIDTH);
116 layout->setColumnStretch (1, 1);
117 layout->setColumnStretch (2, 1);
118 layout->setColumnStretch (3, 0);
119 layout->setColumnMinimumWidth (3, EMPTY_COLUMN_WIDTH);
121 layout->setRowStretch (0, 0);
122 layout->setRowMinimumHeight (0, EMPTY_ROW_HEIGHT);
123 layout->setRowStretch (row, 0);
124 layout->setRowMinimumHeight (row, EMPTY_ROW_HEIGHT);
129 bool DlgSettingsCurveAddRemove::endsWithNumber (
const QString &str)
const
131 bool success =
false;
133 if (!str.isEmpty ()) {
135 success = (str.right (1).at (0).digitValue() >= 0);
143 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::handleOk";
153 void DlgSettingsCurveAddRemove::insertCurveName (
int row,
154 const QString &curveNameNew,
155 const QString &curveNameOriginal,
158 if (m_curveNameList->insertRow (row)) {
160 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::insertCurveName curveName=" << curveNameNew.toLatin1 ().data ();
166 m_curveNameList->
setData (m_curveNameList->index (row, 0),
167 curvesEntry.curveNameCurrent ());
168 m_curveNameList->
setData (m_curveNameList->index (row, 1),
169 curvesEntry.curveNameOriginal ());
170 m_curveNameList->
setData (m_curveNameList->index (row, 2),
175 LOG4CPP_ERROR_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::insertCurveName failed curveName="
176 << curveNameNew.toLatin1 ().data ();
183 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::load";
188 while (m_curveNameList->
rowCount () > 0) {
189 m_curveNameList->removeRow (0);
193 QStringList::const_iterator itr;
194 for (itr = curveNames.begin (); itr != curveNames.end (); itr++) {
195 QString curveName = *itr;
196 appendCurveName (curveName,
204 QString DlgSettingsCurveAddRemove::nextCurveName ()
const
206 const QString DASH_ONE (
"-1");
208 ENGAUGE_CHECK_PTR (m_listCurves);
210 int numSelectedItems = m_listCurves->selectionModel ()->selectedIndexes ().count ();
211 int numItems = m_listCurves->model ()->rowCount ();
214 int currentIndex = -1;
215 if ((numSelectedItems == 0) &&
219 currentIndex = numItems;
221 }
else if (numSelectedItems == 1) {
224 currentIndex = m_listCurves->selectionModel ()->selectedIndexes ().at (0).row ();
229 QString curveNameBefore, curveNameAfter;
230 if (currentIndex > 0) {
232 QModelIndex index = m_curveNameList->index (currentIndex - 1, 0);
233 curveNameBefore = m_curveNameList->
data (index).toString ();
237 if ((0 <= currentIndex) && (currentIndex < numItems)) {
239 QModelIndex index = m_curveNameList->index (currentIndex, 0);
240 curveNameAfter = m_curveNameList->
data (index).toString ();
245 QString curveNameNext;
246 if (curveNameBefore.isEmpty () && !curveNameAfter.isEmpty () && endsWithNumber (curveNameAfter)) {
249 int numberAfter = numberAtEnd (curveNameAfter);
250 int numberNew = numberAfter - 1;
251 int pos = curveNameAfter.lastIndexOf (QString::number (numberAfter));
254 curveNameNext = QString (
"%1%2")
255 .arg (curveNameAfter.left (pos))
260 curveNameNext = curveNameAfter;
264 }
else if (curveNameBefore.isEmpty ()) {
266 curveNameNext = DEFAULT_GRAPH_CURVE_NAME;
270 curveNameNext = curveNameBefore;
272 if (endsWithNumber (curveNameBefore)) {
275 int numberBefore = numberAtEnd (curveNameBefore);
276 int numberNew = numberBefore + 1;
277 int pos = curveNameBefore.lastIndexOf (QString::number (numberBefore));
280 curveNameNext = QString (
"%1%2")
281 .arg (curveNameBefore.left (pos))
283 if (curveNameNext == curveNameAfter) {
286 curveNameNext = QString (
"%1%2")
287 .arg (curveNameBefore)
298 curveNameNext += DASH_ONE;
301 return curveNameNext;
304 int DlgSettingsCurveAddRemove::numberAtEnd (
const QString &str)
const
306 ENGAUGE_ASSERT (endsWithNumber (str));
310 int ch = str.size () - 1;
311 while (str.at (ch).digitValue() >= 0) {
320 return sign * str.mid (ch).toInt ();
323 void DlgSettingsCurveAddRemove::removeSelectedCurves ()
325 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::removeSelectedCurves";
327 for (
int i = m_listCurves->selectionModel ()->selectedIndexes ().count () - 1; i >= 0; i--) {
329 int row = m_listCurves->selectionModel ()->selectedIndexes ().at (i).row ();
331 m_curveNameList->removeRow (row);
335 void DlgSettingsCurveAddRemove::slotDataChanged (
const QModelIndex &topLeft,
336 const QModelIndex &bottomRight,
337 const QVector<int> &roles)
339 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::slotDataChanged"
340 <<
" topLeft=(" << topLeft.row () <<
"," << topLeft.column () <<
")"
341 <<
" bottomRight=(" << bottomRight.row () <<
"," << bottomRight.column () <<
")"
342 <<
" roles=" << rolesAsString (roles).toLatin1 ().data ();
347 void DlgSettingsCurveAddRemove::slotNew ()
349 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::slotNew";
351 const QString NO_ORIGINAL_CURVE_NAME;
352 const int NO_POINTS = 0;
354 QString curveNameSuggestion = nextCurveName ();
356 if (m_listCurves->selectionModel ()->selectedIndexes ().count () == 1) {
358 QModelIndex idx = m_listCurves->selectionModel ()->selectedIndexes ().at (0);
359 insertCurveName (idx.row (),
361 NO_ORIGINAL_CURVE_NAME,
366 appendCurveName (curveNameSuggestion,
367 NO_ORIGINAL_CURVE_NAME,
375 void DlgSettingsCurveAddRemove::slotRemove ()
377 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::slotRemove";
380 for (
int i = 0; i < m_listCurves->selectionModel ()->selectedIndexes ().count (); i++) {
382 int row = m_listCurves->selectionModel ()->selectedIndexes ().at (i).row ();
384 int curvePoints = m_curveNameList->
data (idx, Qt::DisplayRole).toInt ();
386 numPoints += curvePoints;
389 int rtn = QMessageBox::Ok;
393 if (m_listCurves->selectionModel ()->selectedIndexes ().count () == 1) {
394 msg = QString (
"Removing this curve will also remove %1 points. Continue?").arg (numPoints);
396 msg = QString (
"Removing these curves will also remove %1 points. Continue?").arg (numPoints);
399 rtn = QMessageBox::warning (0,
400 "Curves With Points",
403 QMessageBox::Cancel);
406 if (rtn == QMessageBox::Ok) {
407 removeSelectedCurves ();
413 void DlgSettingsCurveAddRemove::slotSelectionChanged (QItemSelection, QItemSelection)
418 void DlgSettingsCurveAddRemove::updateControls ()
420 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::updateControls";
424 ENGAUGE_CHECK_PTR (m_listCurves);
426 int numSelectedItems = m_listCurves->selectionModel ()->selectedIndexes ().count ();
427 int numItems = m_curveNameList->
rowCount ();
429 if (numSelectedItems < 2 ) {
432 m_btnNew->setEnabled (
true);
433 m_btnNew->setText (numSelectedItems == 0 ?
"Add" :
"Insert");
438 m_btnNew->setEnabled (
false);
442 m_btnRemove->setEnabled ((numSelectedItems > 0) && (numSelectedItems < numItems));
void setCmdMediator(CmdMediator &cmdMediator)
Store CmdMediator for easy access by the leaf class.
bool containsCurveNameCurrent(const QString &curveName) const
Return true if specified curve name is already in the list.
Utility class for converting the QVariant in CurveNameList to/from the curve names as QStrings...
Command for DlgSettingsCurveAddRemove.
virtual QWidget * createSubPanel()
Create dialog-specific panel to which base class will add Ok and Cancel buttons.
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Store one curve name data.
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Retrieve data from model.
void finishPanel(QWidget *subPanel)
Add Ok and Cancel buttons to subpanel to get the whole dialog.
static int COL_NUM_POINTS()
Get method for number of points constant.
void load(CmdMediator &cmdMediator)
Load settings from Document.
void enableOk(bool enable)
Let leaf subclass control the Ok button.
Abstract base class for all Settings dialogs.
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
One row per curve name.
Model for DlgSettingsCurveAddRemove and CmdSettingsCurveAddRemove.
MainWindow & mainWindow()
Get method for MainWindow.
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
DlgSettingsCurveAddRemove(MainWindow &mainWindow)
Single constructor.
CmdMediator & cmdMediator()
Provide access to Document information wrapped inside CmdMediator.
virtual void handleOk()
Process slotOk.