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_btnAdd =
new QPushButton (
"Add...");
51 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"
52 "Every curve name must be unique"));
53 m_btnAdd->setSizePolicy (QSizePolicy::Fixed, QSizePolicy::Fixed);
54 connect (m_btnAdd, SIGNAL (released ()),
this, SLOT (slotNew()));
55 layout->addWidget (m_btnAdd, row, 1, 1, 1, Qt::AlignLeft);
57 m_btnRemove =
new QPushButton (
"Remove");
58 m_btnRemove->setWhatsThis (tr (
"Removes the currently selected curve from the curve list.\n\n"
59 "There must always be at least one curve"));
60 m_btnRemove->setSizePolicy (QSizePolicy::Fixed, QSizePolicy::Fixed);
61 connect (m_btnRemove, SIGNAL (released ()),
this, SLOT (slotRemove()));
62 layout->addWidget (m_btnRemove, row++, 2, 1, 1, Qt::AlignRight);
65 void DlgSettingsCurveAddRemove::createListCurves (QGridLayout *layout,
68 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::createListCurves";
70 QLabel *label =
new QLabel (tr (
"Curve Names:"));
71 layout->addWidget (label, row++, 1);
76 m_listCurves =
new QListView;
77 m_listCurves->setWhatsThis (tr (
"List of the curves belonging to this document.\n\n"
78 "Click on a curve name to edit it.\n\n"
79 "Reorder curves by dragging them around."));
80 m_listCurves->setMinimumHeight (200);
81 m_listCurves->setSelectionMode (QAbstractItemView::ExtendedSelection);
82 m_listCurves->setDefaultDropAction (Qt::MoveAction);
83 m_listCurves->setDragDropOverwriteMode (
true);
84 m_listCurves->setDragEnabled (
true);
85 m_listCurves->setDropIndicatorShown (
true);
86 m_listCurves->setDragDropMode (QAbstractItemView::InternalMove);
87 m_listCurves->setViewMode (QListView::ListMode);
88 m_listCurves->setMovement (QListView::Snap);
89 m_listCurves->setModel (m_curveNameList);
90 layout->addWidget (m_listCurves, row++, 1, 1, 2);
91 connect (m_curveNameList, SIGNAL (dataChanged (
const QModelIndex &,
const QModelIndex &,
const QVector<int> &)),
92 this, SLOT (slotDataChanged (
const QModelIndex &,
const QModelIndex &,
const QVector<int> &)));
93 connect (m_listCurves->selectionModel (), SIGNAL (selectionChanged (QItemSelection, QItemSelection)),
94 this, SLOT (slotSelectionChanged (QItemSelection, QItemSelection)));
99 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::createSubPanel";
101 const int EMPTY_COLUMN_WIDTH = 30;
102 const int EMPTY_ROW_HEIGHT = 40;
104 QWidget *subPanel =
new QWidget ();
105 QGridLayout *layout =
new QGridLayout (subPanel);
106 subPanel->setLayout (layout);
109 createListCurves (layout, row);
110 createButtons (layout, row);
112 layout->setColumnStretch (0, 0);
113 layout->setColumnMinimumWidth (0, EMPTY_COLUMN_WIDTH);
114 layout->setColumnStretch (1, 1);
115 layout->setColumnStretch (2, 1);
116 layout->setColumnStretch (3, 0);
117 layout->setColumnMinimumWidth (3, EMPTY_COLUMN_WIDTH);
119 layout->setRowStretch (0, 0);
120 layout->setRowMinimumHeight (0, EMPTY_ROW_HEIGHT);
121 layout->setRowStretch (row, 0);
122 layout->setRowMinimumHeight (row, EMPTY_ROW_HEIGHT);
127 bool DlgSettingsCurveAddRemove::endsWithNumber (
const QString &str)
const
129 bool success =
false;
131 if (!str.isEmpty ()) {
133 success = (str.right (1).at (0).digitValue() >= 0);
141 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::handleOk";
151 void DlgSettingsCurveAddRemove::insertCurveName (
int row,
152 const QString &curveNameNew,
153 const QString &curveNameOriginal,
156 if (m_curveNameList->insertRow (row)) {
158 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::insertCurveName curveName=" << curveNameNew.toLatin1 ().data ();
164 m_curveNameList->
setData (m_curveNameList->index (row, 0),
165 curvesEntry.curveNameCurrent ());
166 m_curveNameList->
setData (m_curveNameList->index (row, 1),
167 curvesEntry.curveNameOriginal ());
168 m_curveNameList->
setData (m_curveNameList->index (row, 2),
173 LOG4CPP_ERROR_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::insertCurveName failed curveName="
174 << curveNameNew.toLatin1 ().data ();
181 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::load";
186 while (m_curveNameList->
rowCount () > 0) {
187 m_curveNameList->removeRow (0);
191 QStringList::const_iterator itr;
192 for (itr = curveNames.begin (); itr != curveNames.end (); itr++) {
193 QString curveName = *itr;
194 appendCurveName (curveName,
202 QString DlgSettingsCurveAddRemove::nextCurveName ()
const
204 const QString DASH_ONE (
"-1");
206 ENGAUGE_CHECK_PTR (m_listCurves);
208 int numSelectedItems = m_listCurves->selectionModel ()->selectedIndexes ().count ();
209 int numItems = m_listCurves->model ()->rowCount ();
212 int currentIndex = -1;
213 if ((numSelectedItems == 0) &&
217 currentIndex = numItems;
219 }
else if (numSelectedItems == 1) {
222 currentIndex = m_listCurves->selectionModel ()->selectedIndexes ().at (0).row ();
227 QString curveNameBefore, curveNameAfter;
228 if (currentIndex > 0) {
230 QModelIndex index = m_curveNameList->index (currentIndex - 1, 0);
231 curveNameBefore = m_curveNameList->
data (index).toString ();
235 if ((0 <= currentIndex) && (currentIndex < numItems)) {
237 QModelIndex index = m_curveNameList->index (currentIndex, 0);
238 curveNameAfter = m_curveNameList->
data (index).toString ();
243 QString curveNameNext;
244 if (curveNameBefore.isEmpty () && !curveNameAfter.isEmpty () && endsWithNumber (curveNameAfter)) {
247 int numberAfter = numberAtEnd (curveNameAfter);
248 int numberNew = numberAfter - 1;
249 int pos = curveNameAfter.lastIndexOf (QString::number (numberAfter));
252 curveNameNext = QString (
"%1%2")
253 .arg (curveNameAfter.left (pos))
258 curveNameNext = curveNameAfter;
262 }
else if (curveNameBefore.isEmpty ()) {
264 curveNameNext = DEFAULT_GRAPH_CURVE_NAME;
268 curveNameNext = curveNameBefore;
270 if (endsWithNumber (curveNameBefore)) {
273 int numberBefore = numberAtEnd (curveNameBefore);
274 int numberNew = numberBefore + 1;
275 int pos = curveNameBefore.lastIndexOf (QString::number (numberBefore));
278 curveNameNext = QString (
"%1%2")
279 .arg (curveNameBefore.left (pos))
281 if (curveNameNext == curveNameAfter) {
284 curveNameNext = QString (
"%1%2")
285 .arg (curveNameBefore)
296 curveNameNext += DASH_ONE;
299 return curveNameNext;
302 int DlgSettingsCurveAddRemove::numberAtEnd (
const QString &str)
const
304 ENGAUGE_ASSERT (endsWithNumber (str));
308 int ch = str.size () - 1;
309 while (str.at (ch).digitValue() >= 0) {
318 return sign * str.mid (ch).toInt ();
321 void DlgSettingsCurveAddRemove::removeSelectedCurves ()
323 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::removeSelectedCurves";
325 for (
int i = m_listCurves->selectionModel ()->selectedIndexes ().count () - 1; i >= 0; i--) {
327 int row = m_listCurves->selectionModel ()->selectedIndexes ().at (i).row ();
329 m_curveNameList->removeRow (row);
333 void DlgSettingsCurveAddRemove::slotDataChanged (
const QModelIndex &topLeft,
334 const QModelIndex &bottomRight,
335 const QVector<int> &roles)
337 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::slotDataChanged"
338 <<
" topLeft=(" << topLeft.row () <<
"," << topLeft.column () <<
")"
339 <<
" bottomRight=(" << bottomRight.row () <<
"," << bottomRight.column () <<
")"
340 <<
" roles=" << rolesAsString (roles).toLatin1 ().data ();
345 void DlgSettingsCurveAddRemove::slotNew ()
347 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::slotNew";
349 const QString NO_ORIGINAL_CURVE_NAME;
350 const int NO_POINTS = 0;
352 QString curveNameSuggestion = nextCurveName ();
354 appendCurveName (curveNameSuggestion,
355 NO_ORIGINAL_CURVE_NAME,
361 void DlgSettingsCurveAddRemove::slotRemove ()
363 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::slotRemove";
366 for (
int i = 0; i < m_listCurves->selectionModel ()->selectedIndexes ().count (); i++) {
368 int row = m_listCurves->selectionModel ()->selectedIndexes ().at (i).row ();
370 int curvePoints = m_curveNameList->
data (idx, Qt::DisplayRole).toInt ();
372 numPoints += curvePoints;
375 int rtn = QMessageBox::Ok;
379 if (m_listCurves->selectionModel ()->selectedIndexes ().count () == 1) {
380 msg = QString (
"Removing this curve will also remove %1 points. Continue?").arg (numPoints);
382 msg = QString (
"Removing these curves will also remove %1 points. Continue?").arg (numPoints);
385 rtn = QMessageBox::warning (0,
386 "Curves With Points",
389 QMessageBox::Cancel);
392 if (rtn == QMessageBox::Ok) {
393 removeSelectedCurves ();
399 void DlgSettingsCurveAddRemove::slotSelectionChanged (QItemSelection, QItemSelection)
404 void DlgSettingsCurveAddRemove::updateControls ()
406 LOG4CPP_INFO_S ((*mainCat)) <<
"DlgSettingsCurveAddRemove::updateControls";
410 ENGAUGE_CHECK_PTR (m_listCurves);
412 int numSelectedItems = m_listCurves->selectionModel ()->selectedIndexes ().count ();
413 int numItems = m_curveNameList->
rowCount ();
416 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.