diff options
author | Tomaz Canabrava <tcanabrava@kde.org> | 2013-09-18 21:56:53 -0300 |
---|---|---|
committer | Tomaz Canabrava <tcanabrava@kde.org> | 2013-09-18 21:56:53 -0300 |
commit | bcefa4baff2140b8eaa8d9222555656284258ffb (patch) | |
tree | 227be7f40e5df7405e9a65872b974bc054908c1e | |
parent | 955c1c1fe5db8ad7381b5d764ae9a66052dc6502 (diff) | |
download | subsurface-bcefa4baff2140b8eaa8d9222555656284258ffb.tar.gz |
Good deal of Code Cleanup for the maintab
Some cleanup was needed in the MainTab code for ages,
now the Edition / Cancel / Ok of the Notes tab is
way more intuitive on the code level.
This is needed to support multidive edition on the
Cylinders and Weigths too, since it's a bit more
complicated, I didn't want to start with a Spaguetti
Code.
Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
-rw-r--r-- | qt-ui/maintab.cpp | 206 | ||||
-rw-r--r-- | qt-ui/maintab.h | 4 | ||||
-rw-r--r-- | qt-ui/maintab.ui | 36 |
3 files changed, 92 insertions, 154 deletions
diff --git a/qt-ui/maintab.cpp b/qt-ui/maintab.cpp index cb61db84f..5905285fa 100644 --- a/qt-ui/maintab.cpp +++ b/qt-ui/maintab.cpp @@ -33,6 +33,7 @@ MainTab::MainTab(QWidget *parent) : QTabWidget(parent), ui->cylinders->setModel(cylindersModel); ui->weights->setModel(weightModel); ui->diveNotesMessage->hide(); + ui->notesButtonBox->hide(); ui->diveNotesMessage->setCloseButtonVisible(false); #ifdef __APPLE__ setDocumentMode(false); @@ -41,17 +42,7 @@ MainTab::MainTab(QWidget *parent) : QTabWidget(parent), #endif // we start out with the fields read-only; once things are // filled from a dive, they are made writeable - ui->location->setReadOnly(true); - ui->coordinates->setReadOnly(true); - ui->divemaster->setReadOnly(true); - ui->buddy->setReadOnly(true); - ui->suit->setReadOnly(true); - ui->notes->setReadOnly(true); - ui->rating->setReadOnly(true); - ui->visibility->setReadOnly(true); - - ui->editAccept->hide(); - ui->editReset->hide(); + setEnabled(false); ui->location->installEventFilter(this); ui->coordinates->installEventFilter(this); @@ -78,7 +69,8 @@ MainTab::MainTab(QWidget *parent) : QTabWidget(parent), connect(ui->cylinders->view(), SIGNAL(clicked(QModelIndex)), this, SLOT(editCylinderWidget(QModelIndex))); connect(ui->weights->view(), SIGNAL(clicked(QModelIndex)), this, SLOT(editWeigthWidget(QModelIndex))); - + connect(ui->notesButtonBox, SIGNAL(accepted()), this, SLOT(acceptChanges())); + connect(ui->notesButtonBox, SIGNAL(rejected()), this, SLOT(rejectChanges())); ui->cylinders->view()->setItemDelegateForColumn(CylindersModel::TYPE, new TankInfoDelegate()); ui->weights->view()->setItemDelegateForColumn(WeightModel::TYPE, new WSInfoDelegate()); @@ -94,22 +86,56 @@ MainTab::MainTab(QWidget *parent) : QTabWidget(parent), void MainTab::enableEdition() { - if (ui->editAccept->isVisible() || !selected_dive) + mainWindow()->dive_list()->setEnabled(false); + if (ui->notesButtonBox->isVisible() || !selected_dive) return; - ui->editAccept->setChecked(true); - ui->editAccept->show(); - ui->editReset->show(); - on_editAccept_clicked(true); + // We may be editing one or more dives here. backup everything. + notesBackup.clear(); + ui->notesButtonBox->show(); + + if (mainWindow() && mainWindow()->dive_list()->selectedTrips.count() == 1) { + // we are editing trip location and notes + ui->diveNotesMessage->setText(tr("This trip is being edited. Select Save or Undo when ready.")); + ui->diveNotesMessage->animatedShow(); + notesBackup[NULL].notes = ui->notes->toPlainText(); + notesBackup[NULL].location = ui->location->text(); + editMode = TRIP; + } else { + ui->diveNotesMessage->setText(tr("This dive is being edited. Select Save or Undo when ready.")); + ui->diveNotesMessage->animatedShow(); + + // We may be editing one or more dives here. backup everything. + struct dive *mydive; + for (int i = 0; i < dive_table.nr; i++) { + mydive = get_dive(i); + if (!mydive) + continue; + if (!mydive->selected) + continue; + + notesBackup[mydive].buddy = QString(mydive->buddy); + notesBackup[mydive].suit = QString(mydive->suit); + notesBackup[mydive].notes = QString(mydive->notes); + notesBackup[mydive].divemaster = QString(mydive->divemaster); + notesBackup[mydive].location = QString(mydive->location); + notesBackup[mydive].rating = mydive->rating; + notesBackup[mydive].visibility = mydive->visibility; + notesBackup[mydive].latitude = mydive->latitude; + notesBackup[mydive].longitude = mydive->longitude; + notesBackup[mydive].coordinates = ui->location->text(); + } + editMode = DIVE; + } } bool MainTab::eventFilter(QObject* object, QEvent* event) { - if (event->type() == QEvent::FocusIn && (object == ui->rating || object == ui->visibility)){ + if (isEnabled() && event->type() == QEvent::FocusIn && (object == ui->rating || object == ui->visibility)){ enableEdition(); } - if (event->type() == QEvent::MouseButtonPress) { + if (isEnabled() && event->type() == QEvent::MouseButtonPress ) { enableEdition(); } return false; // don't "eat" the event. @@ -153,6 +179,9 @@ void MainTab::clearStats() void MainTab::updateDiveInfo(int dive) { + if(!isEnabled()) + setEnabled(true); + editMode = NONE; // This method updates ALL tabs whenever a new dive or trip is // selected. @@ -192,10 +221,8 @@ void MainTab::updateDiveInfo(int dive) ui->visibilityLabel->setVisible(false); // rename the remaining fields and fill data from selected trip dive_trip_t *currentTrip = *mainWindow()->dive_list()->selectedTrips.begin(); - ui->location->setReadOnly(false); ui->LocationLabel->setText(tr("Trip Location")); ui->location->setText(currentTrip->location); - ui->notes->setReadOnly(false); ui->NotesLabel->setText(tr("Trip Notes")); ui->notes->setText(currentTrip->notes); } else { @@ -211,19 +238,11 @@ void MainTab::updateDiveInfo(int dive) ui->visibilityLabel->setVisible(true); ui->BuddyLabel->setVisible(true); ui->DivemasterLabel->setVisible(true); - ui->divemaster->setReadOnly(false); - ui->buddy->setReadOnly(false); - ui->suit->setReadOnly(false); - ui->rating->setReadOnly(false); - ui->visibility->setReadOnly(false); /* and fill them from the dive */ ui->rating->setCurrentStars(d->rating); ui->visibility->setCurrentStars(d->visibility); // reset labels in case we last displayed trip notes - ui->location->setReadOnly(false); - ui->coordinates->setReadOnly(false); ui->LocationLabel->setText(tr("Location")); - ui->notes->setReadOnly(false); ui->NotesLabel->setText(tr("Notes")); } ui->maximumDepthText->setText(get_depth_string(d->maxdepth, TRUE)); @@ -272,15 +291,6 @@ void MainTab::updateDiveInfo(int dive) cylindersModel->setDive(d); weightModel->setDive(d); } else { - /* make the fields read-only */ - ui->location->setReadOnly(true); - ui->coordinates->setReadOnly(true); - ui->divemaster->setReadOnly(true); - ui->buddy->setReadOnly(true); - ui->suit->setReadOnly(true); - ui->notes->setReadOnly(true); - ui->rating->setReadOnly(true); - ui->visibility->setReadOnly(true); /* clear the fields */ ui->rating->setCurrentStars(0); ui->sacText->clear(); @@ -327,86 +337,37 @@ void MainTab::reload() DiveMasterCompletionModel::instance()->updateModel(); } -void MainTab::on_editAccept_clicked(bool edit) +void MainTab::acceptChanges() { - ui->location->setReadOnly(!edit); - ui->coordinates->setReadOnly(!edit); - ui->divemaster->setReadOnly(!edit); - ui->buddy->setReadOnly(!edit); - ui->suit->setReadOnly(!edit); - ui->notes->setReadOnly(!edit); - ui->rating->setReadOnly(!edit); - ui->visibility->setReadOnly(!edit); - - mainWindow()->dive_list()->setEnabled(!edit); - - if (edit) { - - // We may be editing one or more dives here. backup everything. - notesBackup.clear(); + mainWindow()->dive_list()->setEnabled(true); - if (mainWindow() && mainWindow()->dive_list()->selectedTrips.count() == 1) { - // we are editing trip location and notes - ui->diveNotesMessage->setText(tr("This trip is being edited. Select Save or Undo when ready.")); - ui->diveNotesMessage->animatedShow(); - notesBackup[NULL].notes = ui->notes->toPlainText(); - notesBackup[NULL].location = ui->location->text(); - editMode = TRIP; - } else { - ui->diveNotesMessage->setText(tr("This dive is being edited. Select Save or Undo when ready.")); - ui->diveNotesMessage->animatedShow(); - - // We may be editing one or more dives here. backup everything. - struct dive *mydive; - for (int i = 0; i < dive_table.nr; i++) { - mydive = get_dive(i); - if (!mydive) - continue; - if (!mydive->selected) - continue; - - notesBackup[mydive].buddy = QString(mydive->buddy); - notesBackup[mydive].suit = QString(mydive->suit); - notesBackup[mydive].notes = QString(mydive->notes); - notesBackup[mydive].divemaster = QString(mydive->divemaster); - notesBackup[mydive].location = QString(mydive->location); - notesBackup[mydive].rating = mydive->rating; - notesBackup[mydive].visibility = mydive->visibility; - notesBackup[mydive].latitude = mydive->latitude; - notesBackup[mydive].longitude = mydive->longitude; - notesBackup[mydive].coordinates = ui->location->text(); - } - editMode = DIVE; - } + ui->diveNotesMessage->animatedHide(); + ui->notesButtonBox->hide(); + /* now figure out if things have changed */ + if (mainWindow() && mainWindow()->dive_list()->selectedTrips.count() == 1) { + if (notesBackup[NULL].notes != ui->notes->toPlainText() || + notesBackup[NULL].location != ui->location->text()) + mark_divelist_changed(TRUE); } else { - ui->diveNotesMessage->animatedHide(); - ui->editAccept->hide(); - ui->editReset->hide(); - /* now figure out if things have changed */ - if (mainWindow() && mainWindow()->dive_list()->selectedTrips.count() == 1) { - if (notesBackup[NULL].notes != ui->notes->toPlainText() || - notesBackup[NULL].location != ui->location->text()) - mark_divelist_changed(TRUE); - } else { - struct dive *curr = current_dive; - if (notesBackup[curr].buddy != ui->buddy->text() || - notesBackup[curr].suit != ui->suit->text() || - notesBackup[curr].notes != ui->notes->toPlainText() || - notesBackup[curr].divemaster != ui->divemaster->text() || - notesBackup[curr].location != ui->location->text() || - notesBackup[curr].coordinates != ui->coordinates->text() || - notesBackup[curr].rating != ui->visibility->currentStars() || - notesBackup[curr].visibility != ui->rating->currentStars()) - - mark_divelist_changed(TRUE); - if (notesBackup[curr].location != ui->location->text() || - notesBackup[curr].coordinates != ui->coordinates->text()) { - mainWindow()->globe()->reload(); - mainWindow()->globe()->centerOn(current_dive); - } + struct dive *curr = current_dive; + if (notesBackup[curr].buddy != ui->buddy->text() || + notesBackup[curr].suit != ui->suit->text() || + notesBackup[curr].notes != ui->notes->toPlainText() || + notesBackup[curr].divemaster != ui->divemaster->text() || + notesBackup[curr].location != ui->location->text() || + notesBackup[curr].coordinates != ui->coordinates->text() || + notesBackup[curr].rating != ui->visibility->currentStars() || + notesBackup[curr].visibility != ui->rating->currentStars()) + + mark_divelist_changed(TRUE); + if (notesBackup[curr].location != ui->location->text() || + notesBackup[curr].coordinates != ui->coordinates->text()) { + mainWindow()->globe()->reload(); + mainWindow()->globe()->centerOn(current_dive); } - editMode = NONE; } + editMode = NONE; + QPalette p; ui->buddy->setPalette(p); ui->notes->setPalette(p); @@ -425,11 +386,9 @@ void MainTab::on_editAccept_clicked(bool edit) free(what);\ what = strdup(textByteArray.data()); -void MainTab::on_editReset_clicked() +void MainTab::rejectChanges() { - if (!ui->editAccept->isChecked()) - return; - + mainWindow()->dive_list()->setEnabled(true); if (mainWindow() && mainWindow()->dive_list()->selectedTrips.count() == 1){ ui->notes->setText(notesBackup[NULL].notes ); ui->location->setText(notesBackup[NULL].location); @@ -464,21 +423,10 @@ void MainTab::on_editReset_clicked() mydive->visibility = notesBackup[mydive].visibility; } } - ui->editAccept->setChecked(false); ui->diveNotesMessage->animatedHide(); - - ui->location->setReadOnly(true); - ui->coordinates->setReadOnly(true); - ui->divemaster->setReadOnly(true); - ui->buddy->setReadOnly(true); - ui->suit->setReadOnly(true); - ui->notes->setReadOnly(true); - ui->rating->setReadOnly(true); - ui->visibility->setReadOnly(true); mainWindow()->dive_list()->setEnabled(true); - ui->editAccept->hide(); - ui->editReset->hide(); + ui->notesButtonBox->hide(); notesBackup.clear(); QPalette p; ui->buddy->setPalette(p); diff --git a/qt-ui/maintab.h b/qt-ui/maintab.h index 297f27cf9..25fe67743 100644 --- a/qt-ui/maintab.h +++ b/qt-ui/maintab.h @@ -56,8 +56,8 @@ public slots: void addCylinder_clicked(); void addWeight_clicked(); void updateDiveInfo(int dive); - void on_editAccept_clicked(bool edit); - void on_editReset_clicked(); + void acceptChanges(); + void rejectChanges(); void on_location_textChanged(const QString& text); void on_coordinates_textChanged(const QString& text); void on_divemaster_textChanged(const QString& text); diff --git a/qt-ui/maintab.ui b/qt-ui/maintab.ui index e7f18b925..3a988fc3e 100644 --- a/qt-ui/maintab.ui +++ b/qt-ui/maintab.ui @@ -24,14 +24,14 @@ <item row="4" column="0" colspan="2"> <widget class="QLineEdit" name="coordinates"> <property name="readOnly"> - <bool>true</bool> + <bool>false</bool> </property> </widget> </item> <item row="11" column="0" colspan="2"> <widget class="QTextEdit" name="notes"> <property name="readOnly"> - <bool>true</bool> + <bool>false</bool> </property> </widget> </item> @@ -45,28 +45,28 @@ <item row="6" column="0"> <widget class="QLineEdit" name="divemaster"> <property name="readOnly"> - <bool>true</bool> + <bool>false</bool> </property> </widget> </item> <item row="8" column="1"> <widget class="QLineEdit" name="suit"> <property name="readOnly"> - <bool>true</bool> + <bool>false</bool> </property> </widget> </item> <item row="6" column="1"> <widget class="QLineEdit" name="buddy"> <property name="readOnly"> - <bool>true</bool> + <bool>false</bool> </property> </widget> </item> <item row="2" column="0" colspan="2"> <widget class="QLineEdit" name="location"> <property name="readOnly"> - <bool>true</bool> + <bool>false</bool> </property> </widget> </item> @@ -126,30 +126,20 @@ </item> </layout> </item> - <item row="12" column="1"> - <widget class="QPushButton" name="editReset"> - <property name="text"> - <string>Undo</string> - </property> - </widget> - </item> - <item row="12" column="0"> - <widget class="QPushButton" name="editAccept"> + <item row="3" column="0"> + <widget class="QLabel" name="CoordinatedLabel"> <property name="text"> - <string>Save</string> - </property> - <property name="checkable"> - <bool>true</bool> + <string>Coordinates</string> </property> </widget> </item> <item row="0" column="0" colspan="2"> <widget class="KMessageWidget" name="diveNotesMessage" native="true"/> </item> - <item row="3" column="0"> - <widget class="QLabel" name="CoordinatedLabel"> - <property name="text"> - <string>Coordinates</string> + <item row="12" column="0" colspan="2"> + <widget class="QDialogButtonBox" name="notesButtonBox"> + <property name="standardButtons"> + <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set> </property> </widget> </item> |