1 #include "CmdMediator.h"
2 #include "CmdSettingsCurveAddRemove.h"
3 #include "CurveNameList.h"
4 #include "DlgSettingsCurveAddRemove.h"
5 #include "EngaugeAssert.h"
7 #include "MainWindow.h"
10 #include <QGridLayout>
13 #include <QMessageBox>
14 #include <QPushButton>
16 #include <QSpacerItem>
17 #include "QtToString.h"
19 #include "SettingsForGraph.h"
23 "DlgSettingsCurveAddRemove",
26 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::DlgSettingsCurveAddRemove";
32 DlgSettingsCurveAddRemove::~DlgSettingsCurveAddRemove()
34 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::~DlgSettingsCurveAddRemove";
37 void DlgSettingsCurveAddRemove::appendCurveName (
const QString &curveNameNew,
38 const QString &curveNameOriginal,
41 ENGAUGE_CHECK_PTR (m_curveNameList);
43 int row = m_curveNameList->
rowCount ();
50 void DlgSettingsCurveAddRemove::createButtons (QGridLayout *layout,
53 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::createButtons";
55 m_btnAdd =
new QPushButton (
"Add...");
56 m_btnAdd->setWhatsThis (tr (
"Adds a new curve to the curve list. The curve name can be edited in the curve name list.\n\n"
57 "Every curve name must be unique"));
58 m_btnAdd->setSizePolicy (QSizePolicy::Fixed, QSizePolicy::Fixed);
59 connect (m_btnAdd, SIGNAL (released ()),
this, SLOT (slotNew()));
60 layout->addWidget (m_btnAdd, row, 1, 1, 1, Qt::AlignLeft);
62 m_btnRemove =
new QPushButton (
"Remove");
63 m_btnRemove->setWhatsThis (tr (
"Removes the currently selected curve from the curve list.\n\n"
64 "There must always be at least one curve"));
65 m_btnRemove->setSizePolicy (QSizePolicy::Fixed, QSizePolicy::Fixed);
66 connect (m_btnRemove, SIGNAL (released ()),
this, SLOT (slotRemove()));
67 layout->addWidget (m_btnRemove, row++, 2, 1, 1, Qt::AlignRight);
70 void DlgSettingsCurveAddRemove::createListCurves (QGridLayout *layout,
73 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::createListCurves";
75 QLabel *label =
new QLabel (tr (
"Curve Names:"));
76 layout->addWidget (label, row++, 1);
81 m_listCurves =
new QListView;
82 m_listCurves->setWhatsThis (tr (
"List of the curves belonging to this document.\n\n"
83 "Click on a curve name to edit it.\n\n"
84 "Reorder curves by dragging them around."));
85 m_listCurves->setMinimumHeight (200);
86 m_listCurves->setSelectionMode (QAbstractItemView::ExtendedSelection);
87 m_listCurves->setDefaultDropAction (Qt::MoveAction);
88 m_listCurves->setDragDropOverwriteMode (
true);
89 m_listCurves->setDragEnabled (
true);
90 m_listCurves->setDropIndicatorShown (
true);
91 m_listCurves->setDragDropMode (QAbstractItemView::InternalMove);
92 m_listCurves->setViewMode (QListView::ListMode);
93 m_listCurves->setMovement (QListView::Snap);
94 m_listCurves->setModel (m_curveNameList);
95 layout->addWidget (m_listCurves, row++, 1, 1, 2);
96 connect (m_curveNameList, SIGNAL (dataChanged (
const QModelIndex &,
const QModelIndex &,
const QVector<int> &)),
97 this, SLOT (slotDataChanged (
const QModelIndex &,
const QModelIndex &,
const QVector<int> &)));
98 connect (m_listCurves->selectionModel (), SIGNAL (selectionChanged (QItemSelection, QItemSelection)),
99 this, SLOT (slotSelectionChanged (QItemSelection, QItemSelection)));
104 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::createOptionalSaveDefault";
106 m_btnSaveDefault =
new QPushButton (
"Save As Default");
107 m_btnSaveDefault->setWhatsThis (tr (
"Save the curve names for use as defaults for future graph curves."));
108 connect (m_btnSaveDefault, SIGNAL (released ()),
this, SLOT (slotSaveDefault ()));
109 layout->addWidget (m_btnSaveDefault, 0, Qt::AlignLeft);
111 QSpacerItem *spacer =
new QSpacerItem (40, 2);
112 layout->addItem (spacer);
117 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::createSubPanel";
119 const int EMPTY_COLUMN_WIDTH = 30;
121 QWidget *subPanel =
new QWidget ();
122 QGridLayout *layout =
new QGridLayout (subPanel);
123 subPanel->setLayout (layout);
126 createListCurves (layout, row);
127 createButtons (layout, row);
129 layout->setColumnStretch (0, 0);
130 layout->setColumnMinimumWidth (0, EMPTY_COLUMN_WIDTH);
131 layout->setColumnStretch (1, 1);
132 layout->setColumnStretch (2, 1);
133 layout->setColumnStretch (3, 0);
134 layout->setColumnMinimumWidth (3, EMPTY_COLUMN_WIDTH);
139 bool DlgSettingsCurveAddRemove::endsWithNumber (
const QString &str)
const
141 bool success =
false;
143 if (!str.isEmpty ()) {
145 success = (str.right (1).at (0).digitValue() >= 0);
153 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::handleOk";
163 void DlgSettingsCurveAddRemove::insertCurveName (
int row,
164 const QString &curveNameNew,
165 const QString &curveNameOriginal,
168 if (m_curveNameList->insertRow (row)) {
170 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::insertCurveName curveName=" << curveNameNew.toLatin1 ().data ();
176 m_curveNameList->
setData (m_curveNameList->index (row, 0),
177 curvesEntry.curveNameCurrent ());
178 m_curveNameList->
setData (m_curveNameList->index (row, 1),
179 curvesEntry.curveNameOriginal ());
180 m_curveNameList->
setData (m_curveNameList->index (row, 2),
185 LOG4CPP_ERROR_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::insertCurveName failed curveName="
186 << curveNameNew.toLatin1 ().data ();
193 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::load";
198 while (m_curveNameList->
rowCount () > 0) {
199 m_curveNameList->removeRow (0);
203 QStringList::const_iterator itr;
204 for (itr = curveNames.begin (); itr != curveNames.end (); itr++) {
205 QString curveName = *itr;
206 appendCurveName (curveName,
214 QString DlgSettingsCurveAddRemove::nextCurveName ()
const
216 const QString DASH_ONE (
"-1");
218 ENGAUGE_CHECK_PTR (m_listCurves);
220 int numSelectedItems = m_listCurves->selectionModel ()->selectedIndexes ().count ();
221 int numItems = m_listCurves->model ()->rowCount ();
224 int currentIndex = -1;
225 if ((numSelectedItems == 0) &&
229 currentIndex = numItems;
231 }
else if (numSelectedItems == 1) {
234 currentIndex = m_listCurves->selectionModel ()->selectedIndexes ().at (0).row ();
239 QString curveNameBefore, curveNameAfter;
240 if (currentIndex > 0) {
242 QModelIndex index = m_curveNameList->index (currentIndex - 1, 0);
243 curveNameBefore = m_curveNameList->
data (index).toString ();
247 if ((0 <= currentIndex) && (currentIndex < numItems)) {
249 QModelIndex index = m_curveNameList->index (currentIndex, 0);
250 curveNameAfter = m_curveNameList->
data (index).toString ();
255 QString curveNameNext;
256 if (curveNameBefore.isEmpty () && !curveNameAfter.isEmpty () && endsWithNumber (curveNameAfter)) {
259 int numberAfter = numberAtEnd (curveNameAfter);
260 int numberNew = numberAfter - 1;
261 int pos = curveNameAfter.lastIndexOf (QString::number (numberAfter));
264 curveNameNext = QString (
"%1%2")
265 .arg (curveNameAfter.left (pos))
270 curveNameNext = curveNameAfter;
274 }
else if (curveNameBefore.isEmpty ()) {
276 curveNameNext = DEFAULT_GRAPH_CURVE_NAME;
280 curveNameNext = curveNameBefore;
282 if (endsWithNumber (curveNameBefore)) {
285 int numberBefore = numberAtEnd (curveNameBefore);
286 int numberNew = numberBefore + 1;
287 int pos = curveNameBefore.lastIndexOf (QString::number (numberBefore));
290 curveNameNext = QString (
"%1%2")
291 .arg (curveNameBefore.left (pos))
293 if (curveNameNext == curveNameAfter) {
296 curveNameNext = QString (
"%1%2")
297 .arg (curveNameBefore)
306 int indexOneBasedNext = numItems + 1;
314 curveNameNext += DASH_ONE;
317 return curveNameNext;
320 int DlgSettingsCurveAddRemove::numberAtEnd (
const QString &str)
const
322 ENGAUGE_ASSERT (endsWithNumber (str));
326 int ch = str.size () - 1;
327 while (str.at (ch).digitValue() >= 0) {
336 return sign * str.mid (ch).toInt ();
339 void DlgSettingsCurveAddRemove::removeSelectedCurves ()
341 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::removeSelectedCurves";
343 for (
int i = m_listCurves->selectionModel ()->selectedIndexes ().count () - 1; i >= 0; i--) {
345 int row = m_listCurves->selectionModel ()->selectedIndexes ().at (i).row ();
347 m_curveNameList->removeRow (row);
351 void DlgSettingsCurveAddRemove::slotDataChanged (
const QModelIndex &topLeft,
352 const QModelIndex &bottomRight,
353 const QVector<int> &roles)
355 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::slotDataChanged"
356 <<
" topLeft=(" << topLeft.row () <<
"," << topLeft.column () <<
")"
357 <<
" bottomRight=(" << bottomRight.row () <<
"," << bottomRight.column () <<
")"
358 <<
" roles=" << rolesAsString (roles).toLatin1 ().data ();
363 void DlgSettingsCurveAddRemove::slotNew ()
365 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::slotNew";
367 const QString NO_ORIGINAL_CURVE_NAME;
368 const int NO_POINTS = 0;
370 QString curveNameSuggestion = nextCurveName ();
372 appendCurveName (curveNameSuggestion,
373 NO_ORIGINAL_CURVE_NAME,
379 void DlgSettingsCurveAddRemove::slotRemove ()
381 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::slotRemove";
384 for (
int i = 0; i < m_listCurves->selectionModel ()->selectedIndexes ().count (); i++) {
386 int row = m_listCurves->selectionModel ()->selectedIndexes ().at (i).row ();
388 int curvePoints = m_curveNameList->
data (idx, Qt::DisplayRole).toInt ();
390 numPoints += curvePoints;
393 int rtn = QMessageBox::Ok;
397 if (m_listCurves->selectionModel ()->selectedIndexes ().count () == 1) {
398 msg = QString (
"Removing this curve will also remove %1 points. Continue?").arg (numPoints);
400 msg = QString (
"Removing these curves will also remove %1 points. Continue?").arg (numPoints);
403 rtn = QMessageBox::warning (0,
404 "Curves With Points",
407 QMessageBox::Cancel);
410 if (rtn == QMessageBox::Ok) {
411 removeSelectedCurves ();
417 void DlgSettingsCurveAddRemove::slotSaveDefault()
419 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::slotSaveDefault";
421 QSettings settings (SETTINGS_ENGAUGE, SETTINGS_DIGITIZER);
423 for (
int row = 0; row < m_curveNameList->
rowCount (); row++) {
425 QModelIndex idxCurrent = m_curveNameList->index (row, 0);
427 QString curveNameCurrent = m_curveNameList->
data (idxCurrent).toString ();
429 int indexOneBased = row + 1;
434 settings.beginGroup (groupName);
435 settings.setValue (SETTINGS_CURVE_NAME,
437 settings.endGroup ();
441 void DlgSettingsCurveAddRemove::slotSelectionChanged (QItemSelection, QItemSelection)
446 void DlgSettingsCurveAddRemove::updateControls ()
448 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::updateControls";
452 ENGAUGE_CHECK_PTR (m_listCurves);
454 int numSelectedItems = m_listCurves->selectionModel ()->selectedIndexes ().count ();
455 int numItems = m_curveNameList->
rowCount ();
458 m_btnRemove->setEnabled ((numSelectedItems > 0) && (numSelectedItems < numItems));
Manage storage and retrieval of the settings for the curves.
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...
QString defaultCurveName(int indexOneBased, const QString &defaultName) const
Default graph name for the specified curve index.
Command for DlgSettingsCurveAddRemove.
virtual void createOptionalSaveDefault(QHBoxLayout *layout)
Let subclass define an optional Save As Default button.
virtual QWidget * createSubPanel()
Create dialog-specific panel to which base class will add Ok and Cancel buttons.
QString groupNameForNthCurve(int indexOneBased) const
Return the group name, that appears in the settings file/registry, for the specified curve index...
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.