7 #include "CmdMediator.h"
8 #include "CmdSettingsCurveAddRemove.h"
9 #include "CurveNameList.h"
10 #include "DlgSettingsCurveAddRemove.h"
11 #include "EngaugeAssert.h"
13 #include "MainWindow.h"
16 #include <QGridLayout>
19 #include <QMessageBox>
20 #include <QPushButton>
22 #include <QSpacerItem>
23 #include "QtToString.h"
25 #include "SettingsForGraph.h"
29 "DlgSettingsCurveAddRemove",
32 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::DlgSettingsCurveAddRemove";
38 DlgSettingsCurveAddRemove::~DlgSettingsCurveAddRemove()
40 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::~DlgSettingsCurveAddRemove";
43 void DlgSettingsCurveAddRemove::appendCurveName (
const QString &curveNameNew,
44 const QString &curveNameOriginal,
47 ENGAUGE_CHECK_PTR (m_curveNameList);
49 int row = m_curveNameList->
rowCount ();
56 void DlgSettingsCurveAddRemove::createButtons (QGridLayout *layout,
59 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::createButtons";
61 m_btnAdd =
new QPushButton (tr (
"Add..."));
62 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"
63 "Every curve name must be unique"));
64 m_btnAdd->setSizePolicy (QSizePolicy::Fixed, QSizePolicy::Fixed);
65 connect (m_btnAdd, SIGNAL (released ()),
this, SLOT (slotNew()));
66 layout->addWidget (m_btnAdd, row, 1, 1, 1, Qt::AlignLeft);
68 m_btnRemove =
new QPushButton (tr (
"Remove"));
69 m_btnRemove->setWhatsThis (tr (
"Removes the currently selected curve from the curve list.\n\n"
70 "There must always be at least one curve"));
71 m_btnRemove->setSizePolicy (QSizePolicy::Fixed, QSizePolicy::Fixed);
72 connect (m_btnRemove, SIGNAL (released ()),
this, SLOT (slotRemove()));
73 layout->addWidget (m_btnRemove, row++, 2, 1, 1, Qt::AlignRight);
76 void DlgSettingsCurveAddRemove::createListCurves (QGridLayout *layout,
79 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::createListCurves";
81 QLabel *label =
new QLabel (tr (
"Curve Names:"));
82 layout->addWidget (label, row++, 1);
87 m_listCurves =
new QListView;
88 m_listCurves->setWhatsThis (tr (
"List of the curves belonging to this document.\n\n"
89 "Click on a curve name to edit it. Each curve name must be unique.\n\n"
90 "Reorder curves by dragging them around."));
91 m_listCurves->setMinimumHeight (200);
92 m_listCurves->setSelectionMode (QAbstractItemView::ExtendedSelection);
93 m_listCurves->setDefaultDropAction (Qt::MoveAction);
94 m_listCurves->setDragDropOverwriteMode (
true);
95 m_listCurves->setDragEnabled (
true);
96 m_listCurves->setDropIndicatorShown (
true);
97 m_listCurves->setDragDropMode (QAbstractItemView::InternalMove);
98 m_listCurves->setViewMode (QListView::ListMode);
99 m_listCurves->setMovement (QListView::Snap);
100 m_listCurves->setModel (m_curveNameList);
101 layout->addWidget (m_listCurves, row++, 1, 1, 2);
102 connect (m_curveNameList, SIGNAL (dataChanged (
const QModelIndex &,
const QModelIndex &,
const QVector<int> &)),
103 this, SLOT (slotDataChanged (
const QModelIndex &,
const QModelIndex &,
const QVector<int> &)));
104 connect (m_listCurves->selectionModel (), SIGNAL (selectionChanged (QItemSelection, QItemSelection)),
105 this, SLOT (slotSelectionChanged (QItemSelection, QItemSelection)));
110 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::createOptionalSaveDefault";
112 m_btnSaveDefault =
new QPushButton (tr (
"Save As Default"));
113 m_btnSaveDefault->setWhatsThis (tr (
"Save the curve names for use as defaults for future graph curves."));
114 connect (m_btnSaveDefault, SIGNAL (released ()),
this, SLOT (slotSaveDefault ()));
115 layout->addWidget (m_btnSaveDefault, 0, Qt::AlignLeft);
117 QSpacerItem *spacer =
new QSpacerItem (40, 2);
118 layout->addItem (spacer);
123 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::createSubPanel";
125 const int EMPTY_COLUMN_WIDTH = 30;
127 QWidget *subPanel =
new QWidget ();
128 QGridLayout *layout =
new QGridLayout (subPanel);
129 subPanel->setLayout (layout);
132 createListCurves (layout, row);
133 createButtons (layout, row);
135 layout->setColumnStretch (0, 0);
136 layout->setColumnMinimumWidth (0, EMPTY_COLUMN_WIDTH);
137 layout->setColumnStretch (1, 1);
138 layout->setColumnStretch (2, 1);
139 layout->setColumnStretch (3, 0);
140 layout->setColumnMinimumWidth (3, EMPTY_COLUMN_WIDTH);
145 bool DlgSettingsCurveAddRemove::endsWithNumber (
const QString &str)
const
147 bool success =
false;
149 if (!str.isEmpty ()) {
151 success = (str.right (1).at (0).digitValue() >= 0);
159 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::handleOk";
169 void DlgSettingsCurveAddRemove::insertCurveName (
int row,
170 const QString &curveNameNew,
171 const QString &curveNameOriginal,
174 if (m_curveNameList->insertRow (row)) {
176 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::insertCurveName curveName=" << curveNameNew.toLatin1 ().data ();
182 m_curveNameList->
setData (m_curveNameList->index (row, 0),
183 curvesEntry.curveNameCurrent ());
184 m_curveNameList->
setData (m_curveNameList->index (row, 1),
185 curvesEntry.curveNameOriginal ());
186 m_curveNameList->
setData (m_curveNameList->index (row, 2),
191 LOG4CPP_ERROR_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::insertCurveName failed curveName="
192 << curveNameNew.toLatin1 ().data ();
199 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::load";
204 while (m_curveNameList->
rowCount () > 0) {
205 m_curveNameList->removeRow (0);
209 QStringList::const_iterator itr;
210 for (itr = curveNames.begin (); itr != curveNames.end (); itr++) {
211 QString curveName = *itr;
212 appendCurveName (curveName,
220 QString DlgSettingsCurveAddRemove::nextCurveName ()
const
222 const QString DASH_ONE (
"-1");
224 ENGAUGE_CHECK_PTR (m_listCurves);
226 int numSelectedItems = m_listCurves->selectionModel ()->selectedIndexes ().count ();
227 int numItems = m_listCurves->model ()->rowCount ();
230 int currentIndex = -1;
231 if ((numSelectedItems == 0) &&
235 currentIndex = numItems;
237 }
else if (numSelectedItems == 1) {
240 currentIndex = m_listCurves->selectionModel ()->selectedIndexes ().at (0).row ();
245 QString curveNameBefore, curveNameAfter;
246 if (currentIndex > 0) {
248 QModelIndex index = m_curveNameList->index (currentIndex - 1, 0);
249 curveNameBefore = m_curveNameList->
data (index).toString ();
253 if ((0 <= currentIndex) && (currentIndex < numItems)) {
255 QModelIndex index = m_curveNameList->index (currentIndex, 0);
256 curveNameAfter = m_curveNameList->
data (index).toString ();
261 QString curveNameNext;
262 if (curveNameBefore.isEmpty () && !curveNameAfter.isEmpty () && endsWithNumber (curveNameAfter)) {
265 int numberAfter = numberAtEnd (curveNameAfter);
266 int numberNew = numberAfter - 1;
267 int pos = curveNameAfter.lastIndexOf (QString::number (numberAfter));
270 curveNameNext = QString (
"%1%2")
271 .arg (curveNameAfter.left (pos))
276 curveNameNext = curveNameAfter;
280 }
else if (curveNameBefore.isEmpty ()) {
282 curveNameNext = DEFAULT_GRAPH_CURVE_NAME;
286 curveNameNext = curveNameBefore;
288 if (endsWithNumber (curveNameBefore)) {
291 int numberBefore = numberAtEnd (curveNameBefore);
292 int numberNew = numberBefore + 1;
293 int pos = curveNameBefore.lastIndexOf (QString::number (numberBefore));
296 curveNameNext = QString (
"%1%2")
297 .arg (curveNameBefore.left (pos))
299 if (curveNameNext == curveNameAfter) {
302 curveNameNext = QString (
"%1%2")
303 .arg (curveNameBefore)
312 int indexOneBasedNext = numItems + 1;
320 curveNameNext += DASH_ONE;
323 return curveNameNext;
326 int DlgSettingsCurveAddRemove::numberAtEnd (
const QString &str)
const
328 ENGAUGE_ASSERT (endsWithNumber (str));
332 int ch = str.size () - 1;
333 while (str.at (ch).digitValue() >= 0) {
342 return sign * str.mid (ch).toInt ();
345 void DlgSettingsCurveAddRemove::removeSelectedCurves ()
347 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::removeSelectedCurves";
349 for (
int i = m_listCurves->selectionModel ()->selectedIndexes ().count () - 1; i >= 0; i--) {
351 int row = m_listCurves->selectionModel ()->selectedIndexes ().at (i).row ();
353 m_curveNameList->removeRow (row);
357 void DlgSettingsCurveAddRemove::slotDataChanged (
const QModelIndex &topLeft,
358 const QModelIndex &bottomRight,
359 const QVector<int> &roles)
361 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::slotDataChanged"
362 <<
" topLeft=(" << topLeft.row () <<
"," << topLeft.column () <<
")"
363 <<
" bottomRight=(" << bottomRight.row () <<
"," << bottomRight.column () <<
")"
364 <<
" roles=" << rolesAsString (roles).toLatin1 ().data ();
369 void DlgSettingsCurveAddRemove::slotNew ()
371 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::slotNew";
373 const QString NO_ORIGINAL_CURVE_NAME;
374 const int NO_POINTS = 0;
376 QString curveNameSuggestion = nextCurveName ();
378 appendCurveName (curveNameSuggestion,
379 NO_ORIGINAL_CURVE_NAME,
385 void DlgSettingsCurveAddRemove::slotRemove ()
387 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::slotRemove";
390 for (
int i = 0; i < m_listCurves->selectionModel ()->selectedIndexes ().count (); i++) {
392 int row = m_listCurves->selectionModel ()->selectedIndexes ().at (i).row ();
394 int curvePoints = m_curveNameList->
data (idx, Qt::DisplayRole).toInt ();
396 numPoints += curvePoints;
399 int rtn = QMessageBox::Ok;
403 if (m_listCurves->selectionModel ()->selectedIndexes ().count () == 1) {
404 msg = QString (
"%1 %2 %3")
405 .arg (tr (
"Removing this curve will also remove"))
407 .arg (tr (
"points. Continue?"));
409 msg = QString (
"%1 %2 %3")
410 .arg (tr (
"Removing these curves will also remove"))
412 .arg (tr (
"points. Continue?"));
415 rtn = QMessageBox::warning (0,
416 tr (
"Curves With Points"),
419 QMessageBox::Cancel);
422 if (rtn == QMessageBox::Ok) {
423 removeSelectedCurves ();
429 void DlgSettingsCurveAddRemove::slotSaveDefault()
431 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::slotSaveDefault";
433 QSettings settings (SETTINGS_ENGAUGE, SETTINGS_DIGITIZER);
435 for (
int row = 0; row < m_curveNameList->
rowCount (); row++) {
437 QModelIndex idxCurrent = m_curveNameList->index (row, 0);
439 QString curveNameCurrent = m_curveNameList->
data (idxCurrent).toString ();
441 int indexOneBased = row + 1;
446 settings.beginGroup (groupName);
447 settings.setValue (SETTINGS_CURVE_NAME,
449 settings.endGroup ();
453 void DlgSettingsCurveAddRemove::slotSelectionChanged (QItemSelection, QItemSelection)
458 void DlgSettingsCurveAddRemove::updateControls ()
460 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::updateControls";
464 ENGAUGE_CHECK_PTR (m_listCurves);
466 int numSelectedItems = m_listCurves->selectionModel ()->selectedIndexes ().count ();
467 int numItems = m_curveNameList->
rowCount ();
470 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.