From 92397a2bad3d1a2bc261dee67d230e3caa13b8d8 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Sat, 13 Apr 2013 10:17:59 -0300 Subject: Started the real code on the Qt Interface. 1 - Open File already open files, it tries to not break the Gtk version, but some methods on the GTK version still need to be called inside Qt because the code is too tight-coupled. 2 - Close file already close files, same comments for the open file dialog applies here. 3 - The code for adding new cylinders in the cylinder dialog is done, already works and it's integrated with the system. There's a need to implement the edit and delete now, but it will be easyer since I'm starting to not get lost on the code. 4 - Some functions that were used to convert unities have been moved to convert.h ( can be changed later, put there because it's easyer to find something that converts in a convert.h =p ) because they were static functions that operated in the GTK version but I need those functions in the Qt version too. [Dirk Hohndel: lots and lots of whitespace and coding style changes] Signed-off-by: Tomaz Canabrava Signed-off-by: Dirk Hohndel --- qt-ui/models.cpp | 175 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 qt-ui/models.cpp (limited to 'qt-ui/models.cpp') diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp new file mode 100644 index 000000000..dac1c7215 --- /dev/null +++ b/qt-ui/models.cpp @@ -0,0 +1,175 @@ +#include "models.h" +#include "../dive.h" + +CylindersModel::CylindersModel(QObject* parent): QAbstractTableModel(parent) +{ +} + +QVariant CylindersModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + QVariant ret; + if (orientation == Qt::Vertical) { + return ret; + } + + if (role == Qt::DisplayRole) { + switch(section) { + case TYPE: + ret = tr("Type"); + break; + case SIZE: + ret = tr("Size"); + break; + case MAXPRESS: + ret = tr("MaxPress"); + break; + case START: + ret = tr("Start"); + break; + case END: + ret = tr("End"); + break; + case O2: + ret = tr("O2%"); + break; + case HE: + ret = tr("He%"); + break; + } + } + return ret; +} + +int CylindersModel::columnCount(const QModelIndex& parent) const +{ + return 7; +} + +QVariant CylindersModel::data(const QModelIndex& index, int role) const +{ + QVariant ret; + if (!index.isValid() || index.row() >= MAX_CYLINDERS) { + return ret; + } + + dive *d = get_dive(selected_dive); + cylinder_t& cyl = d->cylinder[index.row()]; + + if (role == Qt::DisplayRole) { + switch(index.column()) { + case TYPE: + ret = QString(cyl.type.description); + break; + case SIZE: + ret = cyl.type.size.mliter; + break; + case MAXPRESS: + ret = cyl.type.workingpressure.mbar; + break; + case START: + ret = cyl.start.mbar; + break; + case END: + ret = cyl.end.mbar; + break; + case O2: + ret = cyl.gasmix.o2.permille; + break; + case HE: + ret = cyl.gasmix.he.permille; + break; + } + } + return ret; +} + +int CylindersModel::rowCount(const QModelIndex& parent) const +{ + return usedRows[currentDive]; +} + +void CylindersModel::add(cylinder_t* cyl) +{ + if (usedRows[currentDive] >= MAX_CYLINDERS) { + free(cyl); + } + + int row = usedRows[currentDive]; + + cylinder_t& cylinder = currentDive->cylinder[row]; + + cylinder.end.mbar = cyl->end.mbar; + cylinder.start.mbar = cyl->start.mbar; + + beginInsertRows(QModelIndex(), row, row); + usedRows[currentDive]++; + endInsertRows(); +} + +void CylindersModel::update() +{ + if (usedRows[currentDive] > 0) { + beginRemoveRows(QModelIndex(), 0, usedRows[currentDive]-1); + endRemoveRows(); + } + + currentDive = get_dive(selected_dive); + if (usedRows[currentDive] > 0) { + beginInsertRows(QModelIndex(), 0, usedRows[currentDive]-1); + endInsertRows(); + } +} + +void CylindersModel::clear() +{ + if (usedRows[currentDive] > 0) { + beginRemoveRows(QModelIndex(), 0, usedRows[currentDive]-1); + usedRows[currentDive] = 0; + endRemoveRows(); + } +} + +void WeightModel::clear() +{ +} + +int WeightModel::columnCount(const QModelIndex& parent) const +{ + return 0; +} + +QVariant WeightModel::data(const QModelIndex& index, int role) const +{ + return QVariant(); +} + +int WeightModel::rowCount(const QModelIndex& parent) const +{ + return rows; +} + +QVariant WeightModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + QVariant ret; + if (orientation == Qt::Vertical) { + return ret; + } + + switch(section){ + case TYPE: + ret = tr("Type"); + break; + case WEIGHT: + ret = tr("Weight"); + break; + } + return ret; +} + +void WeightModel::add(weight_t* weight) +{ +} + +void WeightModel::update() +{ +} -- cgit v1.2.3-70-g09d2 From f5c958ad73db696e473aaa35e144f4c9d8ae24de Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Sat, 13 Apr 2013 20:44:02 -0700 Subject: Add Qtr_ macros that uses gettext in a tr() compatible manner This should wrap gettext nicely and replace the "_()" macros we use in C code. Also added comments to the top of all the new files. Suggested-by: Thiago Macieira Signed-off-by: Dirk Hohndel --- Makefile | 6 ++++-- qt-gui.cpp | 5 +++-- qt-ui/addcylinderdialog.cpp | 6 ++++++ qt-ui/addcylinderdialog.h | 6 ++++++ qt-ui/divelistview.cpp | 6 ++++++ qt-ui/divelistview.h | 6 ++++++ qt-ui/divetripmodel.cpp | 17 ++++++++++++----- qt-ui/divetripmodel.h | 6 ++++++ qt-ui/maintab.cpp | 7 +++++++ qt-ui/maintab.h | 6 ++++++ qt-ui/mainwindow.cpp | 15 +++++++++++---- qt-ui/mainwindow.h | 6 ++++++ qt-ui/models.cpp | 25 ++++++++++++++++--------- qt-ui/models.h | 6 ++++++ qt-ui/plotareascene.cpp | 6 ++++++ qt-ui/plotareascene.h | 6 ++++++ 16 files changed, 113 insertions(+), 22 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/Makefile b/Makefile index 8bbd26fb7..e33d92053 100644 --- a/Makefile +++ b/Makefile @@ -110,8 +110,10 @@ else QT_MODULES = QtGui QT_CORE = QtCore endif + +# we need GLIB2CFLAGS for gettext +QTCXXFLAGS = $(shell $(PKGCONFIG) --cflags $(QT_MODULES)) $(GLIB2CFLAGS) LIBQT = $(shell $(PKGCONFIG) --libs $(QT_MODULES)) -QTCXXFLAGS = $(shell $(PKGCONFIG) --cflags $(QT_MODULES)) LIBGTK = $(shell $(PKGCONFIG) --libs gtk+-2.0 glib-2.0) LIBDIVECOMPUTERCFLAGS = $(LIBDIVECOMPUTERINCLUDES) @@ -303,7 +305,7 @@ $(INFOPLIST): $(INFOPLISTINPUT) # Transifex merge the translations update-po-files: - xgettext -o po/subsurface-new.pot -s -k_ -kN_ --keyword=C_:1c,2 --add-comments="++GETTEXT" *.c + xgettext -o po/subsurface-new.pot -s -k_ -kN_ -kQtr_ --keyword=C_:1c,2 --add-comments="++GETTEXT" *.c qt-ui/*.cpp tx push -s tx pull -af diff --git a/qt-gui.cpp b/qt-gui.cpp index 745457763..1621317c2 100644 --- a/qt-gui.cpp +++ b/qt-gui.cpp @@ -25,6 +25,7 @@ #include "version.h" #include "libdivecomputer.h" #include "qt-ui/mainwindow.h" +#include "qt-ui/common.h" #include #include @@ -1784,7 +1785,7 @@ void MainWindow::setCurrentFileName(const QString &fileName) if (fileName == m_currentFileName) return; m_currentFileName = fileName; - QString title = tr("Subsurface"); + QString title = Qtr_("Subsurface"); if (!m_currentFileName.isEmpty()) { QFileInfo fileInfo(m_currentFileName); title += " - " + fileInfo.fileName(); @@ -1797,7 +1798,7 @@ void MainWindow::on_actionOpen_triggered() QString defaultFileName = QString::fromUtf8(prefs.default_filename); QFileInfo fileInfo(defaultFileName); - QFileDialog dialog(this, tr("Open File"), fileInfo.path()); + QFileDialog dialog(this, Qtr_("Open File"), fileInfo.path()); dialog.setFileMode(QFileDialog::ExistingFile); dialog.selectFile(defaultFileName); dialog.setNameFilters(fileNameFilters()); diff --git a/qt-ui/addcylinderdialog.cpp b/qt-ui/addcylinderdialog.cpp index 6f2294a25..043f29907 100644 --- a/qt-ui/addcylinderdialog.cpp +++ b/qt-ui/addcylinderdialog.cpp @@ -1,3 +1,9 @@ +/* + * addcylinderdialog.cpp + * + * classes for the add cylinder dialog of Subsurface + * + */ #include "addcylinderdialog.h" #include "ui_addcylinderdialog.h" #include diff --git a/qt-ui/addcylinderdialog.h b/qt-ui/addcylinderdialog.h index b32494c05..652f7b362 100644 --- a/qt-ui/addcylinderdialog.h +++ b/qt-ui/addcylinderdialog.h @@ -1,3 +1,9 @@ +/* + * addcylinderdialog.h + * + * header file for the add cylinder dialog of Subsurface + * + */ #ifndef ADDCYLINDERDIALOG_H #define ADDCYLINDERDIALOG_H diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp index eafbdd384..a8b1eff05 100644 --- a/qt-ui/divelistview.cpp +++ b/qt-ui/divelistview.cpp @@ -1,3 +1,9 @@ +/* + * divelistview.cpp + * + * classes for the divelist of Subsurface + * + */ #include "divelistview.h" DiveListView::DiveListView(QWidget *parent) : QTreeView(parent) diff --git a/qt-ui/divelistview.h b/qt-ui/divelistview.h index 3ac123a14..be9774c5c 100644 --- a/qt-ui/divelistview.h +++ b/qt-ui/divelistview.h @@ -1,3 +1,9 @@ +/* + * divelistview.h + * + * header file for the dive list of Subsurface + * + */ #ifndef DIVELISTVIEW_H #define DIVELISTVIEW_H diff --git a/qt-ui/divetripmodel.cpp b/qt-ui/divetripmodel.cpp index a03603bcf..0a2944d8b 100644 --- a/qt-ui/divetripmodel.cpp +++ b/qt-ui/divetripmodel.cpp @@ -1,3 +1,10 @@ +/* + * divetripmodel.cpp + * + * classes for the dive trip list in Subsurface + */ + +#include "common.h" #include "divetripmodel.h" @@ -63,15 +70,15 @@ QVariant DiveTripModel::headerData(int section, Qt::Orientation orientation, int { if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { if (section == DIVE_NUMBER) { - return tr("Dive number"); + return Qtr_("Dive number"); } else if (section == DIVE_DATE_TIME) { - return tr("Date"); + return Qtr_("Date"); } else if (section == DIVE_DURATION) { - return tr("Duration"); + return Qtr_("Duration"); } else if (section == DIVE_DEPTH) { - return tr("Depth"); + return Qtr_("Depth"); } else if (section == DIVE_LOCATION) { - return tr("Location"); + return Qtr_("Location"); } } return QVariant(); diff --git a/qt-ui/divetripmodel.h b/qt-ui/divetripmodel.h index 8c8a829e2..ad1815798 100644 --- a/qt-ui/divetripmodel.h +++ b/qt-ui/divetripmodel.h @@ -1,3 +1,9 @@ +/* + * divetripmodel.h + * + * header file for the divetrip model of Subsurface + * + */ #ifndef DIVETRIPMODEL_H #define DIVETRIPMODEL_H diff --git a/qt-ui/maintab.cpp b/qt-ui/maintab.cpp index b957fd1c7..72d8dfebc 100644 --- a/qt-ui/maintab.cpp +++ b/qt-ui/maintab.cpp @@ -1,3 +1,10 @@ +/* + * maintab.cpp + * + * classes for the "notebook" area of the main window of Subsurface + * + */ +#include "common.h" #include "maintab.h" #include "ui_maintab.h" #include "addcylinderdialog.h" diff --git a/qt-ui/maintab.h b/qt-ui/maintab.h index 0e9f285ac..44815fafc 100644 --- a/qt-ui/maintab.h +++ b/qt-ui/maintab.h @@ -1,3 +1,9 @@ +/* + * maintab.h + * + * header file for the main tab of Subsurface + * + */ #ifndef MAINTAB_H #define MAINTAB_H diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp index fdc823d9c..d1cde044a 100644 --- a/qt-ui/mainwindow.cpp +++ b/qt-ui/mainwindow.cpp @@ -1,3 +1,10 @@ +/* + * mainwindow.cpp + * + * classes for the main UI window in Subsurface + */ + +#include "common.h" #include "mainwindow.h" #include "ui_mainwindow.h" @@ -53,7 +60,7 @@ void MainWindow::on_actionNew_triggered() void MainWindow::on_actionOpen_triggered() { - QString filename = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::homePath(), filter()); + QString filename = QFileDialog::getOpenFileName(this, Qtr_("Open File"), QDir::homePath(), filter()); if (filename.isEmpty()){ return; } @@ -283,10 +290,10 @@ QString MainWindow::filter() bool MainWindow::askSaveChanges() { - QString message = ! existing_filename ? tr("You have unsaved changes\nWould you like to save those before closing the datafile?") - : tr("You have unsaved changes to file: %1 \nWould you like to save those before closing the datafile?").arg(existing_filename); + QString message = ! existing_filename ? Qtr_("You have unsaved changes\nWould you like to save those before closing the datafile?") + : Qtr_("You have unsaved changes to file: %1 \nWould you like to save those before closing the datafile?").arg(existing_filename); - if (QMessageBox::question(this, tr("Save Changes?"), message) == QMessageBox::Ok){ + if (QMessageBox::question(this, Qtr_("Save Changes?"), message) == QMessageBox::Ok){ // WARNING: Port. // file_save(NULL,NULL); return true; diff --git a/qt-ui/mainwindow.h b/qt-ui/mainwindow.h index 34acf2b67..43ebde7f5 100644 --- a/qt-ui/mainwindow.h +++ b/qt-ui/mainwindow.h @@ -1,3 +1,9 @@ +/* + * mainwindow.h + * + * header file for the main window of Subsurface + * + */ #ifndef MAINWINDOW_H #define MAINWINDOW_H diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index dac1c7215..a341c0c70 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -1,3 +1,10 @@ +/* + * models.cpp + * + * classes for the equipment models of Subsurface + * + */ +#include "common.h" #include "models.h" #include "../dive.h" @@ -15,25 +22,25 @@ QVariant CylindersModel::headerData(int section, Qt::Orientation orientation, in if (role == Qt::DisplayRole) { switch(section) { case TYPE: - ret = tr("Type"); + ret = Qtr_("Type"); break; case SIZE: - ret = tr("Size"); + ret = Qtr_("Size"); break; case MAXPRESS: - ret = tr("MaxPress"); + ret = Qtr_("MaxPress"); break; case START: - ret = tr("Start"); + ret = Qtr_("Start"); break; case END: - ret = tr("End"); + ret = Qtr_("End"); break; case O2: - ret = tr("O2%"); + ret = Qtr_("O2%"); break; case HE: - ret = tr("He%"); + ret = Qtr_("He%"); break; } } @@ -157,10 +164,10 @@ QVariant WeightModel::headerData(int section, Qt::Orientation orientation, int r switch(section){ case TYPE: - ret = tr("Type"); + ret = Qtr_("Type"); break; case WEIGHT: - ret = tr("Weight"); + ret = Qtr_("Weight"); break; } return ret; diff --git a/qt-ui/models.h b/qt-ui/models.h index 0d0c7b41d..697096f92 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -1,3 +1,9 @@ +/* + * models.h + * + * header file for the equipment models of Subsurface + * + */ #ifndef MODELS_H #define MODELS_H diff --git a/qt-ui/plotareascene.cpp b/qt-ui/plotareascene.cpp index e69de29bb..a728040f5 100644 --- a/qt-ui/plotareascene.cpp +++ b/qt-ui/plotareascene.cpp @@ -0,0 +1,6 @@ +/* + * plotareascene.cpp + * + * classes for profile plot area scene of Subsurface + * + */ diff --git a/qt-ui/plotareascene.h b/qt-ui/plotareascene.h index e69de29bb..a5b07d1be 100644 --- a/qt-ui/plotareascene.h +++ b/qt-ui/plotareascene.h @@ -0,0 +1,6 @@ +/* + * plotareascene.h + * + * header file for the profile plot area scene of Subsurface + * + */ -- cgit v1.2.3-70-g09d2 From d8e11439ad27063b0dad05b2f8f0f4cd7f3e7de1 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Sun, 14 Apr 2013 06:44:29 -0700 Subject: Undoing the last Qtr_ hack The Qtr_ hack isn't needed as in commit 720fc15b2dcd ("Introduce QApplication") had already made sure that we are using gettext. I didn't revert the two commits as I wanted to keep the added header comments and fix the tooling in the Makefile as well. Signed-off-by: Dirk Hohndel --- Makefile | 2 +- qt-gui.cpp | 5 ++--- qt-ui/common.h | 20 -------------------- qt-ui/divetripmodel.cpp | 12 +++++------- qt-ui/maintab.cpp | 1 - qt-ui/mainwindow.cpp | 10 ++++------ qt-ui/models.cpp | 19 +++++++++---------- 7 files changed, 21 insertions(+), 48 deletions(-) delete mode 100644 qt-ui/common.h (limited to 'qt-ui/models.cpp') diff --git a/Makefile b/Makefile index e33d92053..7778d2aa8 100644 --- a/Makefile +++ b/Makefile @@ -305,7 +305,7 @@ $(INFOPLIST): $(INFOPLISTINPUT) # Transifex merge the translations update-po-files: - xgettext -o po/subsurface-new.pot -s -k_ -kN_ -kQtr_ --keyword=C_:1c,2 --add-comments="++GETTEXT" *.c qt-ui/*.cpp + xgettext -o po/subsurface-new.pot -s -k_ -kN_ -ktr --keyword=C_:1c,2 --add-comments="++GETTEXT" *.c qt-ui/*.cpp tx push -s tx pull -af diff --git a/qt-gui.cpp b/qt-gui.cpp index 1621317c2..745457763 100644 --- a/qt-gui.cpp +++ b/qt-gui.cpp @@ -25,7 +25,6 @@ #include "version.h" #include "libdivecomputer.h" #include "qt-ui/mainwindow.h" -#include "qt-ui/common.h" #include #include @@ -1785,7 +1784,7 @@ void MainWindow::setCurrentFileName(const QString &fileName) if (fileName == m_currentFileName) return; m_currentFileName = fileName; - QString title = Qtr_("Subsurface"); + QString title = tr("Subsurface"); if (!m_currentFileName.isEmpty()) { QFileInfo fileInfo(m_currentFileName); title += " - " + fileInfo.fileName(); @@ -1798,7 +1797,7 @@ void MainWindow::on_actionOpen_triggered() QString defaultFileName = QString::fromUtf8(prefs.default_filename); QFileInfo fileInfo(defaultFileName); - QFileDialog dialog(this, Qtr_("Open File"), fileInfo.path()); + QFileDialog dialog(this, tr("Open File"), fileInfo.path()); dialog.setFileMode(QFileDialog::ExistingFile); dialog.selectFile(defaultFileName); dialog.setNameFilters(fileNameFilters()); diff --git a/qt-ui/common.h b/qt-ui/common.h deleted file mode 100644 index 6e00c80ed..000000000 --- a/qt-ui/common.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - * common.h - * - * shared by all Qt/C++ code - * - */ - -#ifndef COMMON_H -#define COMMON_H - -#include -#include - -/* use this instead of tr() for translated string literals */ -inline QString Qtr_(const char *str) -{ - return QString::fromUtf8(gettext(str)); -} - -#endif diff --git a/qt-ui/divetripmodel.cpp b/qt-ui/divetripmodel.cpp index 0a2944d8b..5082494a0 100644 --- a/qt-ui/divetripmodel.cpp +++ b/qt-ui/divetripmodel.cpp @@ -3,8 +3,6 @@ * * classes for the dive trip list in Subsurface */ - -#include "common.h" #include "divetripmodel.h" @@ -70,15 +68,15 @@ QVariant DiveTripModel::headerData(int section, Qt::Orientation orientation, int { if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { if (section == DIVE_NUMBER) { - return Qtr_("Dive number"); + return tr("Dive number"); } else if (section == DIVE_DATE_TIME) { - return Qtr_("Date"); + return tr("Date"); } else if (section == DIVE_DURATION) { - return Qtr_("Duration"); + return tr("Duration"); } else if (section == DIVE_DEPTH) { - return Qtr_("Depth"); + return tr("Depth"); } else if (section == DIVE_LOCATION) { - return Qtr_("Location"); + return tr("Location"); } } return QVariant(); diff --git a/qt-ui/maintab.cpp b/qt-ui/maintab.cpp index 72d8dfebc..53926cb25 100644 --- a/qt-ui/maintab.cpp +++ b/qt-ui/maintab.cpp @@ -4,7 +4,6 @@ * classes for the "notebook" area of the main window of Subsurface * */ -#include "common.h" #include "maintab.h" #include "ui_maintab.h" #include "addcylinderdialog.h" diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp index d1cde044a..577b7fb67 100644 --- a/qt-ui/mainwindow.cpp +++ b/qt-ui/mainwindow.cpp @@ -3,8 +3,6 @@ * * classes for the main UI window in Subsurface */ - -#include "common.h" #include "mainwindow.h" #include "ui_mainwindow.h" @@ -60,7 +58,7 @@ void MainWindow::on_actionNew_triggered() void MainWindow::on_actionOpen_triggered() { - QString filename = QFileDialog::getOpenFileName(this, Qtr_("Open File"), QDir::homePath(), filter()); + QString filename = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::homePath(), filter()); if (filename.isEmpty()){ return; } @@ -290,10 +288,10 @@ QString MainWindow::filter() bool MainWindow::askSaveChanges() { - QString message = ! existing_filename ? Qtr_("You have unsaved changes\nWould you like to save those before closing the datafile?") - : Qtr_("You have unsaved changes to file: %1 \nWould you like to save those before closing the datafile?").arg(existing_filename); + QString message = ! existing_filename ? tr("You have unsaved changes\nWould you like to save those before closing the datafile?") + : tr("You have unsaved changes to file: %1 \nWould you like to save those before closing the datafile?").arg(existing_filename); - if (QMessageBox::question(this, Qtr_("Save Changes?"), message) == QMessageBox::Ok){ + if (QMessageBox::question(this, tr("Save Changes?"), message) == QMessageBox::Ok){ // WARNING: Port. // file_save(NULL,NULL); return true; diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index a341c0c70..64fa6bac3 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -4,7 +4,6 @@ * classes for the equipment models of Subsurface * */ -#include "common.h" #include "models.h" #include "../dive.h" @@ -22,25 +21,25 @@ QVariant CylindersModel::headerData(int section, Qt::Orientation orientation, in if (role == Qt::DisplayRole) { switch(section) { case TYPE: - ret = Qtr_("Type"); + ret = tr("Type"); break; case SIZE: - ret = Qtr_("Size"); + ret = tr("Size"); break; case MAXPRESS: - ret = Qtr_("MaxPress"); + ret = tr("MaxPress"); break; case START: - ret = Qtr_("Start"); + ret = tr("Start"); break; case END: - ret = Qtr_("End"); + ret = tr("End"); break; case O2: - ret = Qtr_("O2%"); + ret = tr("O2%"); break; case HE: - ret = Qtr_("He%"); + ret = tr("He%"); break; } } @@ -164,10 +163,10 @@ QVariant WeightModel::headerData(int section, Qt::Orientation orientation, int r switch(section){ case TYPE: - ret = Qtr_("Type"); + ret = tr("Type"); break; case WEIGHT: - ret = Qtr_("Weight"); + ret = tr("Weight"); break; } return ret; -- cgit v1.2.3-70-g09d2 From 115ee47bfc0aa8ca2b2bdaca047ccf595bbb7120 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Mon, 15 Apr 2013 15:04:35 -0300 Subject: Added the code that will load and populate the Tank Info Added the code that will load and populate the Tank Info ComboBox that`s used by the user to select the Cylinder description. Code curerntly implements more than the GTK version since the GTK version of it was a plain-list, this one is a table based model that can be used in ListViews ( like we use now in the ComboBox ) but also in TableViews ( if there`s a need in the future to see everything that`s catalogued in the Tank Info struct. ) Signed-off-by: Tomaz Canabrava Signed-off-by: Dirk Hohndel --- Makefile | 2 +- dive.h | 13 ++++++ equipment.c | 5 +-- qt-ui/addcylinderdialog.cpp | 4 +- qt-ui/addcylinderdialog.h | 3 ++ qt-ui/models.cpp | 103 +++++++++++++++++++++++++++++++++++++++++++- qt-ui/models.h | 20 +++++++++ 7 files changed, 143 insertions(+), 7 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/Makefile b/Makefile index d64be2edf..5984d0123 100644 --- a/Makefile +++ b/Makefile @@ -149,7 +149,7 @@ ifneq (,$(filter $(UNAME),linux kfreebsd gnu)) GCONF2CFLAGS = $(shell $(PKGCONFIG) --cflags gconf-2.0) OSSUPPORT = linux OSSUPPORT_CFLAGS = $(GTKCFLAGS) $(GCONF2CFLAGS) - ifneq ($(findstring reduce_relocations, $(shell $(PKGCONFIG) --variable qt_config $(QT_CORE))),) + ifneq ($(filter reduce_relocations, $(shell $(PKGCONFIG) --variable qt_config $(QT_CORE))), ) CXXFLAGS += -fPIE endif else ifeq ($(UNAME), darwin) diff --git a/dive.h b/dive.h index 734aa2269..c276fe6de 100644 --- a/dive.h +++ b/dive.h @@ -697,6 +697,19 @@ void get_gas_string(int o2, int he, char *buf, int len); struct event *get_next_event(struct event *event, char *name); + +/* this struct holds the information that + * describes the cylinders of air. + * it is a global variable initialized in equipment.c + * used to fill the combobox in the add/edit cylinder + * dialog + */ + +struct tank_info { + const char *name; + int cuft, ml, psi, bar; +}; + #ifdef DEBUGFILE extern char *debugfilename; extern FILE *debugfile; diff --git a/equipment.c b/equipment.c index a1d156f94..d126b4327 100644 --- a/equipment.c +++ b/equipment.c @@ -790,10 +790,7 @@ static void record_weightsystem_changes(weightsystem_t *ws, struct ws_widget *we * we should pick up any other names from the dive * logs directly. */ -static struct tank_info { - const char *name; - int cuft, ml, psi, bar; -} tank_info[100] = { +struct tank_info tank_info[100] = { /* Need an empty entry for the no-cylinder case */ { "", }, diff --git a/qt-ui/addcylinderdialog.cpp b/qt-ui/addcylinderdialog.cpp index 043f29907..5b91617ed 100644 --- a/qt-ui/addcylinderdialog.cpp +++ b/qt-ui/addcylinderdialog.cpp @@ -9,11 +9,13 @@ #include #include #include "../conversions.h" - +#include "models.h" AddCylinderDialog::AddCylinderDialog(QWidget *parent) : ui(new Ui::AddCylinderDialog()) +, tankInfoModel(new TankInfoModel()) { ui->setupUi(this); + ui->cylinderType->setModel(tankInfoModel); } void AddCylinderDialog::setCylinder(cylinder_t *cylinder) diff --git a/qt-ui/addcylinderdialog.h b/qt-ui/addcylinderdialog.h index 652f7b362..fc68faa72 100644 --- a/qt-ui/addcylinderdialog.h +++ b/qt-ui/addcylinderdialog.h @@ -14,6 +14,8 @@ namespace Ui{ class AddCylinderDialog; } +class TankInfoModel; + class AddCylinderDialog : public QDialog{ Q_OBJECT public: @@ -24,6 +26,7 @@ public: private: Ui::AddCylinderDialog *ui; cylinder_t *currentCylinder; + TankInfoModel *tankInfoModel; }; diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 64fa6bac3..d1b8dc0a0 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -7,6 +7,8 @@ #include "models.h" #include "../dive.h" +extern struct tank_info tank_info[100]; + CylindersModel::CylindersModel(QObject* parent): QAbstractTableModel(parent) { } @@ -161,7 +163,7 @@ QVariant WeightModel::headerData(int section, Qt::Orientation orientation, int r return ret; } - switch(section){ + switch(section) { case TYPE: ret = tr("Type"); break; @@ -179,3 +181,102 @@ void WeightModel::add(weight_t* weight) void WeightModel::update() { } + +void TankInfoModel::add(const QString& description) +{ + // When the user `creates` a new one on the combobox. + // for now, empty till dirk cleans the GTK code. +} + +void TankInfoModel::clear() +{ +} + +int TankInfoModel::columnCount(const QModelIndex& parent) const +{ + return 3; +} + +QVariant TankInfoModel::data(const QModelIndex& index, int role) const +{ + QVariant ret; + if (!index.isValid()) { + return ret; + } + struct tank_info *info = &tank_info[index.row()]; + + int ml = info->ml; + + int bar = ((info->psi) ? psi_to_bar(info->psi) : info->bar) * 1000 + 0.5; + + if (info->cuft) { + double airvolume = cuft_to_l(info->cuft) * 1000.0; + ml = airvolume / bar_to_atm(bar) + 0.5; + } + if (role == Qt::DisplayRole) { + switch(index.column()) { + case BAR: ret = bar; + break; + case ML: ret = ml; + break; + case DESCRIPTION: + ret = QString(info->name); + break; + } + } + return ret; +} + +QVariant TankInfoModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + QVariant ret; + + if (orientation != Qt::Horizontal) + return ret; + + if (role == Qt::DisplayRole) { + switch(section) { + case BAR: + ret = tr("Bar"); + break; + case ML: + ret = tr("Ml"); + break; + case DESCRIPTION: + ret = tr("Description"); + break; + } + } + return ret; +} + +int TankInfoModel::rowCount(const QModelIndex& parent) const +{ + return rows+1; +} + +TankInfoModel::TankInfoModel() : QAbstractTableModel(), rows(-1) +{ + struct tank_info *info = tank_info; + for (info = tank_info ; info->name; info++, rows++); + + if (rows > -1) { + beginInsertRows(QModelIndex(), 0, rows); + endInsertRows(); + } +} + +void TankInfoModel::update() +{ + if(rows > -1) { + beginRemoveRows(QModelIndex(), 0, rows); + endRemoveRows(); + } + struct tank_info *info = tank_info; + for (info = tank_info ; info->name; info++, rows++); + + if (rows > -1) { + beginInsertRows(QModelIndex(), 0, rows); + endInsertRows(); + } +} diff --git a/qt-ui/models.h b/qt-ui/models.h index 697096f92..8d86102cb 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -10,6 +10,26 @@ #include #include "../dive.h" +/* Encapsulates the tank_info global variable + * to show on Qt`s Model View System.*/ +class TankInfoModel : public QAbstractTableModel { +Q_OBJECT +public: + enum { DESCRIPTION, ML, BAR}; + TankInfoModel(); + + /*reimp*/ QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + /*reimp*/ int columnCount(const QModelIndex& parent = QModelIndex()) const; + /*reimp*/ QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const; + /*reimp*/ int rowCount(const QModelIndex& parent = QModelIndex()) const; + + void add(const QString& description); + void clear(); + void update(); +private: + int rows; +}; + class CylindersModel : public QAbstractTableModel { Q_OBJECT public: -- cgit v1.2.3-70-g09d2 From a0280ae7d2cdd483bc53c7bc91a8aa438f9234de Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Sun, 21 Apr 2013 22:12:36 -0300 Subject: Move model related code from MainWindow and adjustments. Moves the DiveTrip model related code to models.h The DiveTripModel was implemented in three parts: DiveTripModel.h DiveTripModel.cpp MainWindow.cpp (the code to populate the model) This patch changes the DiveTripModel from it's original implementation to the file models.h, wich should store all models (Dirk requested the Qt developers to not create 2 files per class, but instead to use a file for functionality, and data-models are one functionality.) Besides that, this code cleans up a bit the style: removed operator<< for .push_back, const references where they apply, moved the internal DiveItem class to the .cpp since it should be visible only to the DiveTripModel class, and redesigned the current interface of the model to be identical of the GTK one (used the UTF8-star and 2 subscribed, for instance). Amit (the creator of the original code) should comment here if it's ok with my changes. Signed-off-by: Tomaz Canabrava Signed-off-by: Dirk Hohndel --- Makefile | 2 +- qt-ui/divetripmodel.cpp | 141 ------------------------------ qt-ui/divetripmodel.h | 86 ------------------- qt-ui/mainwindow.cpp | 41 +-------- qt-ui/models.cpp | 224 ++++++++++++++++++++++++++++++++++++++++++++++++ qt-ui/models.h | 34 +++++++- 6 files changed, 259 insertions(+), 269 deletions(-) delete mode 100644 qt-ui/divetripmodel.cpp delete mode 100644 qt-ui/divetripmodel.h (limited to 'qt-ui/models.cpp') diff --git a/Makefile b/Makefile index 5984d0123..7b7ec0476 100644 --- a/Makefile +++ b/Makefile @@ -187,7 +187,7 @@ MSGOBJS=$(addprefix share/locale/,$(MSGLANGS:.po=.UTF-8/LC_MESSAGES/subsurface.m QTOBJS = qt-ui/maintab.o qt-ui/mainwindow.o qt-ui/plotareascene.o qt-ui/divelistview.o \ - qt-ui/divetripmodel.o qt-ui/addcylinderdialog.o qt-ui/models.o + qt-ui/addcylinderdialog.o qt-ui/models.o GTKOBJS = info-gtk.o divelist-gtk.o planner-gtk.o statistics-gtk.o diff --git a/qt-ui/divetripmodel.cpp b/qt-ui/divetripmodel.cpp deleted file mode 100644 index 5082494a0..000000000 --- a/qt-ui/divetripmodel.cpp +++ /dev/null @@ -1,141 +0,0 @@ -/* - * divetripmodel.cpp - * - * classes for the dive trip list in Subsurface - */ -#include "divetripmodel.h" - - -DiveItem::DiveItem(int num, QString dt, float dur, float dep, QString loc, DiveItem *p): - number(num), dateTime(dt), duration(dur), depth(dep), location(loc), parentItem(p) -{ - if (parentItem) - parentItem->addChild(this); -} - - -DiveTripModel::DiveTripModel(const QString &filename, QObject *parent) : QAbstractItemModel(parent), filename(filename) -{ - rootItem = new DiveItem; -} - - -Qt::ItemFlags DiveTripModel::flags(const QModelIndex &index) const -{ - Qt::ItemFlags diveFlags = QAbstractItemModel::flags(index); - if (index.isValid()) { - diveFlags |= Qt::ItemIsSelectable|Qt::ItemIsEnabled; - } - return diveFlags; -} - - -QVariant DiveTripModel::data(const QModelIndex &index, int role) const -{ - if (!index.isValid()) - return QVariant(); - - if (role != Qt::DisplayRole) - return QVariant(); - - DiveItem *item = static_cast(index.internalPointer()); - - QVariant retVal; - switch (index.column()) { - case DIVE_NUMBER: - retVal = QVariant(item->diveNumber()); - break; - case DIVE_DATE_TIME: - retVal = QVariant(item->diveDateTime()); - break; - case DIVE_DURATION: - retVal = QVariant(item->diveDuration()); - break; - case DIVE_DEPTH: - retVal = QVariant(item->diveDepth()); - break; - case DIVE_LOCATION: - retVal = QVariant(item->diveLocation()); - break; - default: - return QVariant(); - }; - return retVal; -} - - -QVariant DiveTripModel::headerData(int section, Qt::Orientation orientation, int role) const -{ - if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { - if (section == DIVE_NUMBER) { - return tr("Dive number"); - } else if (section == DIVE_DATE_TIME) { - return tr("Date"); - } else if (section == DIVE_DURATION) { - return tr("Duration"); - } else if (section == DIVE_DEPTH) { - return tr("Depth"); - } else if (section == DIVE_LOCATION) { - return tr("Location"); - } - } - return QVariant(); -} - -int DiveTripModel::rowCount(const QModelIndex &parent) const -{ - /* only allow kids in column 0 */ - if (parent.isValid() && parent.column() > 0){ - return 0; - } - DiveItem *item = itemForIndex(parent); - return item ? item->childCount() : 0; -} - - - -int DiveTripModel::columnCount(const QModelIndex &parent) const -{ - return parent.isValid() && parent.column() != 0 ? 0 : COLUMNS; -} - - -QModelIndex DiveTripModel::index(int row, int column, const QModelIndex &parent) const -{ - - if (!rootItem || row < 0 || column < 0 || column >= COLUMNS || - (parent.isValid() && parent.column() != 0)) - return QModelIndex(); - - DiveItem *parentItem = itemForIndex(parent); - Q_ASSERT(parentItem); - if (DiveItem *item = parentItem->childAt(row)){ - return createIndex(row, column, item); - } - return QModelIndex(); -} - - -QModelIndex DiveTripModel::parent(const QModelIndex &childIndex) const -{ - if (!childIndex.isValid()) - return QModelIndex(); - - DiveItem *child = static_cast(childIndex.internalPointer()); - DiveItem *parent = child->parent(); - - if (parent == rootItem) - return QModelIndex(); - - return createIndex(parent->rowOfChild(child), 0, parent); -} - - -DiveItem* DiveTripModel::itemForIndex(const QModelIndex &index) const -{ - if (index.isValid()) { - DiveItem *item = static_cast(index.internalPointer()); - return item; - } - return rootItem; -} diff --git a/qt-ui/divetripmodel.h b/qt-ui/divetripmodel.h deleted file mode 100644 index ad1815798..000000000 --- a/qt-ui/divetripmodel.h +++ /dev/null @@ -1,86 +0,0 @@ -/* - * divetripmodel.h - * - * header file for the divetrip model of Subsurface - * - */ -#ifndef DIVETRIPMODEL_H -#define DIVETRIPMODEL_H - -#include - -/*! A DiveItem for use with a DiveTripModel - * - * A simple class which wraps basic stats for a dive (e.g. duration, depth) and - * tidies up after it's children. This is done manually as we don't inherit from - * QObject. - * -*/ -class DiveItem -{ -public: - explicit DiveItem(): number(0), dateTime(QString()), duration(0.0), depth(0.0), location(QString()) {parentItem = 0;} - explicit DiveItem(int num, QString dt, float, float, QString loc, DiveItem *parent = 0); - ~DiveItem() { qDeleteAll(childlist); } - - int diveNumber() const { return number; } - QString diveDateTime() const { return dateTime; } - float diveDuration() const { return duration; } - float diveDepth() const { return depth; } - QString diveLocation() const { return location; } - - DiveItem *parent() const { return parentItem; } - DiveItem *childAt(int row) const { return childlist.value(row); } - int rowOfChild(DiveItem *child) const { return childlist.indexOf(child); } - int childCount() const { return childlist.count(); } - bool hasChildren() const { return !childlist.isEmpty(); } - QList children() const { return childlist; } - void addChild(DiveItem* item) { item->parentItem = this; childlist << item; } /* parent = self */ - - -private: - - int number; - QString dateTime; - float duration; - float depth; - QString location; - - DiveItem *parentItem; - QList childlist; - -}; - - -enum Column {DIVE_NUMBER, DIVE_DATE_TIME, DIVE_DURATION, DIVE_DEPTH, DIVE_LOCATION, COLUMNS}; - - -/*! An AbstractItemModel for recording dive trip information such as a list of dives. -* -*/ -class DiveTripModel : public QAbstractItemModel -{ -public: - - DiveTripModel(const QString &filename, QObject *parent = 0); - - Qt::ItemFlags flags(const QModelIndex &index) const; - QVariant data(const QModelIndex &index, int role) const; - QVariant headerData(int section, Qt::Orientation orientation, int role) const; - int rowCount(const QModelIndex &parent) const; - - int columnCount(const QModelIndex &parent = QModelIndex()) const; - virtual QModelIndex index(int row, int column, - const QModelIndex &parent = QModelIndex()) const; - virtual QModelIndex parent(const QModelIndex &child) const; - - DiveItem *itemForIndex(const QModelIndex &) const; - -private: - - DiveItem *rootItem; - QString filename; - -}; - -#endif // DIVETRIPMODEL_H diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp index 551c28faa..46ce076d4 100644 --- a/qt-ui/mainwindow.cpp +++ b/qt-ui/mainwindow.cpp @@ -13,7 +13,6 @@ #include #include "divelistview.h" -#include "divetripmodel.h" #include "glib.h" #include "../dive.h" @@ -21,45 +20,11 @@ #include "../pref.h" -MainWindow::MainWindow() : ui(new Ui::MainWindow()) +MainWindow::MainWindow() : ui(new Ui::MainWindow()), + model(new DiveTripModel(this)) { ui->setupUi(this); - - /* may want to change ctor to avoid filename as 1st param. - * here we just use an empty string - */ - model = new DiveTripModel("",this); - if (model) { - ui->ListWidget->setModel(model); - } - /* we need root to parent all top level dives - * trips need more work as it complicates parent/child stuff. - * - * Todo: look at alignment/format of e.g. duration in view - * - */ - DiveItem *dive = 0; - DiveItem *root = model->itemForIndex(QModelIndex()); - if (root) { - int i; - Q_UNUSED(dive) - - struct dive *d; - qDebug("address of dive_table %p", &dive_table); - for_each_dive(i, d) { - struct tm tm; - char *buffer; - utc_mkdate(d->when, &tm); - buffer = get_dive_date_string(&tm); - dive = new DiveItem(d->number, - buffer, - (float)d->duration.seconds/60, - (float)d->maxdepth.mm/1000 , - d->location, - root); - free(buffer); - } - } + ui->ListWidget->setModel(model); } void MainWindow::on_actionNew_triggered() diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index d1b8dc0a0..40307d022 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -6,6 +6,7 @@ */ #include "models.h" #include "../dive.h" +#include "../divelist.h" extern struct tank_info tank_info[100]; @@ -280,3 +281,226 @@ void TankInfoModel::update() endInsertRows(); } } + +/*! A DiveItem for use with a DiveTripModel + * + * A simple class which wraps basic stats for a dive (e.g. duration, depth) and + * tidies up after it's children. This is done manually as we don't inherit from + * QObject. + * +*/ +class DiveItem +{ +public: + explicit DiveItem(): number(0), dateTime(QString()), duration(0.0), depth(0.0), location(QString()) {parentItem = 0;} + explicit DiveItem(int num, QString dt, float, float, QString loc, DiveItem *parent = 0); + ~DiveItem() { qDeleteAll(childlist); } + + int diveNumber() const { return number; } + const QString& diveDateTime() const { return dateTime; } + float diveDuration() const { return duration; } + float diveDepth() const { return depth; } + const QString& diveLocation() const { return location; } + DiveItem *parent() const { return parentItem; } + const QList& children() const { return childlist; } + + void addChild(DiveItem* item) { + item->parentItem = this; + childlist.push_back(item); + } /* parent = self */ + +private: + int number; + QString dateTime; + float duration; + float depth; + QString location; + + DiveItem *parentItem; + QList childlist; + +}; + +DiveItem::DiveItem(int num, QString dt, float dur, float dep, QString loc, DiveItem *p): + number(num), dateTime(dt), duration(dur), depth(dep), location(loc), parentItem(p) +{ + if (parentItem) + parentItem->addChild(this); +} + +DiveTripModel::DiveTripModel(QObject *parent) : QAbstractItemModel(parent) +{ + rootItem = new DiveItem; + int i; + struct dive *d; + + for_each_dive(i, d) { + struct tm tm; + char *buffer; + utc_mkdate(d->when, &tm); + buffer = get_dive_date_string(&tm); + new DiveItem(d->number, + buffer, + d->duration.seconds/60.0, + d->maxdepth.mm/1000.0 , + d->location, + rootItem); + free(buffer); + } +} + + +Qt::ItemFlags DiveTripModel::flags(const QModelIndex &index) const +{ + Qt::ItemFlags diveFlags = QAbstractItemModel::flags(index); + if (index.isValid()) { + diveFlags |= Qt::ItemIsSelectable|Qt::ItemIsEnabled; + } + return diveFlags; +} + + +QVariant DiveTripModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + DiveItem *item = static_cast(index.internalPointer()); + + QVariant retVal; + if (role == Qt::DisplayRole){ + switch (index.column()) { + case NR: + retVal = item->diveNumber(); + break; + case DATE: + retVal = item->diveDateTime(); + break; + case DURATION: + retVal = item->diveDuration(); + break; + case DEPTH: + retVal = item->diveDepth(); + break; + case LOCATION: + retVal = item->diveLocation(); + break; + } + } + return retVal; +} + + +QVariant DiveTripModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + QVariant ret; + if (orientation != Qt::Horizontal){ + return ret; + } + + if (role == Qt::DisplayRole) { + switch(section){ + case NR: + ret = tr("#"); + break; + case DATE: + ret = tr("Date"); + break; + case RATING: + ret = UTF8_BLACKSTAR; + break; + case DEPTH: + ret = tr("ft"); + break; + case DURATION: + ret = tr("min"); + break; + case TEMPERATURE: + ret = UTF8_DEGREE "F"; + break; + case TOTALWEIGHT: + ret = tr("lbs"); + break; + case SUIT: + ret = tr("Suit"); + break; + case CYLINDER: + ret = tr("Cyl"); + break; + case NITROX: + ret = "O" UTF8_SUBSCRIPT_2 "%"; + break; + case SAC: + ret = tr("SAC"); + break; + case OTU: + ret = tr("OTU"); + break; + case MAXCNS: + ret = tr("maxCNS"); + break; + case LOCATION: + ret = tr("Location"); + break; + } + } + return ret; +} + +int DiveTripModel::rowCount(const QModelIndex &parent) const +{ + /* only allow kids in column 0 */ + if (parent.isValid() && parent.column() > 0){ + return 0; + } + DiveItem *item = itemForIndex(parent); + return item ? item->children().count() : 0; +} + + + +int DiveTripModel::columnCount(const QModelIndex &parent) const +{ + return parent.isValid() && parent.column() != 0 ? 0 : COLUMNS; +} + + +QModelIndex DiveTripModel::index(int row, int column, const QModelIndex &parent) const +{ + + if (!rootItem || row < 0 || column < 0 || column >= COLUMNS || + (parent.isValid() && parent.column() != 0)) + return QModelIndex(); + + DiveItem *parentItem = itemForIndex(parent); + Q_ASSERT(parentItem); + if (DiveItem *item = parentItem->children().at(row)){ + return createIndex(row, column, item); + } + return QModelIndex(); +} + + +QModelIndex DiveTripModel::parent(const QModelIndex &childIndex) const +{ + if (!childIndex.isValid()) + return QModelIndex(); + + DiveItem *child = static_cast(childIndex.internalPointer()); + DiveItem *parent = child->parent(); + + if (parent == rootItem) + return QModelIndex(); + + return createIndex(parent->children().indexOf(child), 0, parent); +} + + +DiveItem* DiveTripModel::itemForIndex(const QModelIndex &index) const +{ + if (index.isValid()) { + DiveItem *item = static_cast(index.internalPointer()); + return item; + } + return rootItem; +} diff --git a/qt-ui/models.h b/qt-ui/models.h index 8d86102cb..d64faf0bd 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -15,7 +15,7 @@ class TankInfoModel : public QAbstractTableModel { Q_OBJECT public: - enum { DESCRIPTION, ML, BAR}; + enum Column { DESCRIPTION, ML, BAR}; TankInfoModel(); /*reimp*/ QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; @@ -30,10 +30,12 @@ private: int rows; }; +/* Encapsulation of the Cylinder Model, that presents the + * Current cylinders that are used on a dive. */ class CylindersModel : public QAbstractTableModel { Q_OBJECT public: - enum {TYPE, SIZE, MAXPRESS, START, END, O2, HE}; + enum Column {TYPE, SIZE, MAXPRESS, START, END, O2, HE}; explicit CylindersModel(QObject* parent = 0); /*reimp*/ QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; @@ -53,8 +55,10 @@ private: QMap usedRows; }; +/* Encapsulation of the Weight Model, that represents + * the current weights on a dive. */ class WeightModel : public QAbstractTableModel { - enum{TYPE, WEIGHT}; + enum Column {TYPE, WEIGHT}; /*reimp*/ QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; /*reimp*/ int columnCount(const QModelIndex& parent = QModelIndex()) const; /*reimp*/ QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const; @@ -67,4 +71,28 @@ private: int rows; }; +/*! An AbstractItemModel for recording dive trip information such as a list of dives. +* +*/ +class DiveItem; // Represents a single item on the model, implemented in the .cpp since it's private for this class. +class DiveTripModel : public QAbstractItemModel +{ +public: + enum Column {NR, DATE, RATING, DEPTH, DURATION, TEMPERATURE, TOTALWEIGHT, SUIT, CYLINDER, NITROX, SAC, OTU, MAXCNS, LOCATION, COLUMNS }; + + DiveTripModel(QObject *parent = 0); + + /*reimp*/ Qt::ItemFlags flags(const QModelIndex &index) const; + /*reimp*/ QVariant data(const QModelIndex &index, int role) const; + /*reimp*/ QVariant headerData(int section, Qt::Orientation orientation, int role) const; + /*reimp*/ int rowCount(const QModelIndex &parent) const; + /*reimp*/ int columnCount(const QModelIndex &parent = QModelIndex()) const; + /*reimp*/ QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; + /*reimp*/ QModelIndex parent(const QModelIndex &child) const; + +private: + DiveItem *itemForIndex(const QModelIndex& index) const; + DiveItem *rootItem; +}; + #endif -- cgit v1.2.3-70-g09d2 From edab566105eba559c1a3976f18ca9be0a05db43b Mon Sep 17 00:00:00 2001 From: Amit Chaudhuri Date: Wed, 24 Apr 2013 16:57:30 +0100 Subject: Amend divetrip model to use int units Amend the DiveItem class to avoid float in favour of int. Add getters which return display friendly QStrings reflecting user preferences for (e.g.) depth. Modify DiveTripModel to support controlled alignment by column; right align for depth and duration. Fix problems with utf8 encoding on rating stars, degree symbols and O2 subscript. Signed-off-by: Amit Chaudhuri Signed-off-by: Dirk Hohndel --- qt-ui/divelistview.cpp | 1 + qt-ui/models.cpp | 110 +++++++++++++++++++++++++++++++++++++------------ 2 files changed, 85 insertions(+), 26 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp index a8b1eff05..676d7c463 100644 --- a/qt-ui/divelistview.cpp +++ b/qt-ui/divelistview.cpp @@ -8,4 +8,5 @@ DiveListView::DiveListView(QWidget *parent) : QTreeView(parent) { + setUniformRowHeights(true); } diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 40307d022..64ba01349 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -8,6 +8,8 @@ #include "../dive.h" #include "../divelist.h" +#include + extern struct tank_info tank_info[100]; CylindersModel::CylindersModel(QObject* parent): QAbstractTableModel(parent) @@ -292,14 +294,16 @@ void TankInfoModel::update() class DiveItem { public: - explicit DiveItem(): number(0), dateTime(QString()), duration(0.0), depth(0.0), location(QString()) {parentItem = 0;} - explicit DiveItem(int num, QString dt, float, float, QString loc, DiveItem *parent = 0); + explicit DiveItem(): number(0), dateTime(QString()), seconds(0), mm(0), location(QString()) { parentItem = 0; } + explicit DiveItem(int num, QString dt, int, int, QString loc, DiveItem *parent = 0); ~DiveItem() { qDeleteAll(childlist); } int diveNumber() const { return number; } const QString& diveDateTime() const { return dateTime; } - float diveDuration() const { return duration; } - float diveDepth() const { return depth; } + int diveDuration() const { return seconds; } + int diveDepth() const { return mm; } + QString displayDuration() const; + QString displayDepth() const; const QString& diveLocation() const { return location; } DiveItem *parent() const { return parentItem; } const QList& children() const { return childlist; } @@ -312,8 +316,8 @@ public: private: int number; QString dateTime; - float duration; - float depth; + int seconds; + int mm; QString location; DiveItem *parentItem; @@ -321,13 +325,49 @@ private: }; -DiveItem::DiveItem(int num, QString dt, float dur, float dep, QString loc, DiveItem *p): - number(num), dateTime(dt), duration(dur), depth(dep), location(loc), parentItem(p) +DiveItem::DiveItem(int num, QString dt, int dur, int dep, QString loc, DiveItem *p): + number(num), dateTime(dt), seconds(dur), mm(dep), location(loc), parentItem(p) { if (parentItem) parentItem->addChild(this); } +QString DiveItem::displayDepth() const +{ + const int scale = 1000; + QString fract, str; + if (get_units()->length == units::METERS) { + fract = QString::number((unsigned)(mm % scale) / 10); + str = QString("%1.%2").arg((unsigned)(mm / scale)).arg(fract); + } + if (get_units()->length == units::FEET) { + str = QString::number(mm_to_feet(mm),'f',2); + } + return str; +} + +QString DiveItem::displayDuration() const +{ + int hrs, mins, secs, val; + const int minutes_hour = 60; + const int seconds_minute= 60; + + val = seconds; + secs = seconds % seconds_minute; + val /= seconds_minute; + mins = val % seconds_minute; + val /= minutes_hour; + hrs = val % minutes_hour; + + QString displayTime; + if (hrs > 0) + displayTime = QString("%1:%2:%3").arg(hrs).arg(mins).arg(secs); + else + displayTime = QString("%1:%2").arg(mins).arg(secs); + + return displayTime; +} + DiveTripModel::DiveTripModel(QObject *parent) : QAbstractItemModel(parent) { rootItem = new DiveItem; @@ -341,8 +381,8 @@ DiveTripModel::DiveTripModel(QObject *parent) : QAbstractItemModel(parent) buffer = get_dive_date_string(&tm); new DiveItem(d->number, buffer, - d->duration.seconds/60.0, - d->maxdepth.mm/1000.0 , + d->duration.seconds, + d->maxdepth.mm, d->location, rootItem); free(buffer); @@ -368,7 +408,17 @@ QVariant DiveTripModel::data(const QModelIndex &index, int role) const DiveItem *item = static_cast(index.internalPointer()); QVariant retVal; - if (role == Qt::DisplayRole){ + if (role == Qt::TextAlignmentRole) { + switch (index.column()) { + case DURATION: /* fall through */ + case DEPTH: + retVal = Qt::AlignRight; + break; + default: + retVal = Qt::AlignLeft; + } + } + if (role == Qt::DisplayRole) { switch (index.column()) { case NR: retVal = item->diveNumber(); @@ -377,10 +427,12 @@ QVariant DiveTripModel::data(const QModelIndex &index, int role) const retVal = item->diveDateTime(); break; case DURATION: - retVal = item->diveDuration(); + retVal = item->displayDuration(); + //retVal = item->diveDuration(); break; case DEPTH: - retVal = item->diveDepth(); + retVal = item->displayDepth(); + //retVal = item->diveDepth(); break; case LOCATION: retVal = item->diveLocation(); @@ -394,12 +446,11 @@ QVariant DiveTripModel::data(const QModelIndex &index, int role) const QVariant DiveTripModel::headerData(int section, Qt::Orientation orientation, int role) const { QVariant ret; - if (orientation != Qt::Horizontal){ + if (orientation != Qt::Horizontal) return ret; - } if (role == Qt::DisplayRole) { - switch(section){ + switch(section) { case NR: ret = tr("#"); break; @@ -407,19 +458,28 @@ QVariant DiveTripModel::headerData(int section, Qt::Orientation orientation, int ret = tr("Date"); break; case RATING: - ret = UTF8_BLACKSTAR; + ret = QString::fromUtf8(UTF8_BLACKSTAR); break; case DEPTH: - ret = tr("ft"); + if (get_units()->length == units::METERS) + ret = tr("Depth (m)"); + else + ret = tr("Depth (ft)"); break; case DURATION: - ret = tr("min"); + ret = tr("Duration (h:mm:ss)"); break; case TEMPERATURE: - ret = UTF8_DEGREE "F"; + if (get_units()->temperature == units::CELSIUS) + ret = QString("%1%2").arg(QString::fromUtf8(UTF8_DEGREE)).arg("C"); + else + ret = QString("%1%2").arg(QString::fromUtf8(UTF8_DEGREE)).arg("F"); break; case TOTALWEIGHT: - ret = tr("lbs"); + if (get_units()->weight == units::KG) + ret = tr("Weight (kg)"); + else + ret = tr("Weight (lbs)"); break; case SUIT: ret = tr("Suit"); @@ -428,7 +488,7 @@ QVariant DiveTripModel::headerData(int section, Qt::Orientation orientation, int ret = tr("Cyl"); break; case NITROX: - ret = "O" UTF8_SUBSCRIPT_2 "%"; + ret = QString("O%1%").arg(QString::fromUtf8(UTF8_SUBSCRIPT_2)); break; case SAC: ret = tr("SAC"); @@ -450,9 +510,8 @@ QVariant DiveTripModel::headerData(int section, Qt::Orientation orientation, int int DiveTripModel::rowCount(const QModelIndex &parent) const { /* only allow kids in column 0 */ - if (parent.isValid() && parent.column() > 0){ + if (parent.isValid() && parent.column() > 0) return 0; - } DiveItem *item = itemForIndex(parent); return item ? item->children().count() : 0; } @@ -474,9 +533,8 @@ QModelIndex DiveTripModel::index(int row, int column, const QModelIndex &parent) DiveItem *parentItem = itemForIndex(parent); Q_ASSERT(parentItem); - if (DiveItem *item = parentItem->children().at(row)){ + if (DiveItem *item = parentItem->children().at(row)) return createIndex(row, column, item); - } return QModelIndex(); } -- cgit v1.2.3-70-g09d2 From e156b00f05c99b990a5e482c23e172e168d80b74 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Wed, 24 Apr 2013 16:02:41 -0700 Subject: Fix column headings for divelist This looks better and is consistent with the Gtk version. Signed-off-by: Dirk Hohndel --- qt-ui/models.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 64ba01349..31610facd 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -462,12 +462,12 @@ QVariant DiveTripModel::headerData(int section, Qt::Orientation orientation, int break; case DEPTH: if (get_units()->length == units::METERS) - ret = tr("Depth (m)"); + ret = tr("m"); else - ret = tr("Depth (ft)"); + ret = tr("ft"); break; case DURATION: - ret = tr("Duration (h:mm:ss)"); + ret = tr("min"); break; case TEMPERATURE: if (get_units()->temperature == units::CELSIUS) @@ -477,9 +477,9 @@ QVariant DiveTripModel::headerData(int section, Qt::Orientation orientation, int break; case TOTALWEIGHT: if (get_units()->weight == units::KG) - ret = tr("Weight (kg)"); + ret = tr("kg"); else - ret = tr("Weight (lbs)"); + ret = tr("lbs"); break; case SUIT: ret = tr("Suit"); -- cgit v1.2.3-70-g09d2 From ce235bd58133fd0ebde4d7156dd5cb4c62abeb60 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Wed, 24 Apr 2013 16:03:14 -0700 Subject: Correctly format depth and time We really need those leading 0s. Signed-off-by: Dirk Hohndel --- qt-ui/models.cpp | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 31610facd..e7f4b1f8b 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -338,7 +338,7 @@ QString DiveItem::displayDepth() const QString fract, str; if (get_units()->length == units::METERS) { fract = QString::number((unsigned)(mm % scale) / 10); - str = QString("%1.%2").arg((unsigned)(mm / scale)).arg(fract); + str = QString("%1.%2").arg((unsigned)(mm / scale)).arg(fract, 2, QChar('0')); } if (get_units()->length == units::FEET) { str = QString::number(mm_to_feet(mm),'f',2); @@ -348,23 +348,19 @@ QString DiveItem::displayDepth() const QString DiveItem::displayDuration() const { - int hrs, mins, secs, val; - const int minutes_hour = 60; - const int seconds_minute= 60; + int hrs, mins, secs; - val = seconds; - secs = seconds % seconds_minute; - val /= seconds_minute; - mins = val % seconds_minute; - val /= minutes_hour; - hrs = val % minutes_hour; + secs = seconds % 60; + mins = seconds / 60; + hrs = mins / 60; + mins -= hrs * 60; QString displayTime; - if (hrs > 0) - displayTime = QString("%1:%2:%3").arg(hrs).arg(mins).arg(secs); + if (hrs) + displayTime = QString("%1:%2:").arg(hrs).arg(mins, 2, 10, QChar('0')); else - displayTime = QString("%1:%2").arg(mins).arg(secs); - + displayTime = QString("%1:").arg(mins); + displayTime += QString("%1").arg(secs, 2, 10, QChar('0')); return displayTime; } -- cgit v1.2.3-70-g09d2 From fde0f49df899a3c591e8def6dda93a4d83bbc962 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Wed, 24 Apr 2013 23:21:57 -0700 Subject: Use Subsurface's data structures for DiveItem We have these data structures for a reason. They provide context about the units used and prevent mistakes. And of course they are used everywhere else so we should use them here, too. This also tries to display some more data and make things look a bit more like the Gtk version when it comes to alignment and formatting. My guess is this will make Qt developers' eyes bleed. My apologies. Signed-off-by: Dirk Hohndel --- qt-ui/models.cpp | 151 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 116 insertions(+), 35 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index e7f4b1f8b..cf6490051 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -294,17 +294,28 @@ void TankInfoModel::update() class DiveItem { public: - explicit DiveItem(): number(0), dateTime(QString()), seconds(0), mm(0), location(QString()) { parentItem = 0; } - explicit DiveItem(int num, QString dt, int, int, QString loc, DiveItem *parent = 0); + explicit DiveItem(): number(0), when(), duration(), maxdepth(), rating(0), + temperature(), totalweight(), suit(QString()), sac(0), + otu(0), maxcns(0), location(QString()) { parentItem = 0; } + explicit DiveItem(int, timestamp_t, duration_t, depth_t, int, temperature_t, + weight_t, QString, int, int, int, QString, DiveItem *parent = 0); ~DiveItem() { qDeleteAll(childlist); } int diveNumber() const { return number; } - const QString& diveDateTime() const { return dateTime; } - int diveDuration() const { return seconds; } - int diveDepth() const { return mm; } + const QString diveDateTime() const { return QString::fromUtf8(get_dive_date_string(when)); } + int diveDuration() const { return duration.seconds; } + int diveDepth() const { return maxdepth.mm; } + int diveSac() const { return sac; } + int diveOtu() const { return otu; } + int diveMaxcns() const { return maxcns; } + int diveWeight() const { return totalweight.grams; } QString displayDuration() const; QString displayDepth() const; + QString displayTemperature() const; + QString displayWeight() const; + QString displaySac() const; const QString& diveLocation() const { return location; } + const QString& diveSuit() const { return suit; } DiveItem *parent() const { return parentItem; } const QList& children() const { return childlist; } @@ -315,19 +326,30 @@ public: private: int number; - QString dateTime; - int seconds; - int mm; + timestamp_t when; + duration_t duration; + depth_t maxdepth; + int rating; + temperature_t temperature; + weight_t totalweight; + QString suit; + int sac; + int otu; + int maxcns; QString location; - DiveItem *parentItem; QList childlist; - }; -DiveItem::DiveItem(int num, QString dt, int dur, int dep, QString loc, DiveItem *p): - number(num), dateTime(dt), seconds(dur), mm(dep), location(loc), parentItem(p) +DiveItem::DiveItem(int num, timestamp_t when, duration_t duration, depth_t maxdepth, int rating, temperature_t temp, + weight_t weight, QString su, int sac, int otu, int maxcns, QString loc, DiveItem *p): + number(num), rating(rating), suit(su), sac(sac), otu(otu), maxcns(maxcns), location(loc), parentItem(p) { + this->when = when; + this->duration = duration; + this->maxdepth = maxdepth; + this->temperature = temp; + this->totalweight = weight; if (parentItem) parentItem->addChild(this); } @@ -337,11 +359,11 @@ QString DiveItem::displayDepth() const const int scale = 1000; QString fract, str; if (get_units()->length == units::METERS) { - fract = QString::number((unsigned)(mm % scale) / 10); - str = QString("%1.%2").arg((unsigned)(mm / scale)).arg(fract, 2, QChar('0')); + fract = QString::number((unsigned)(maxdepth.mm % scale) / 10); + str = QString("%1.%2").arg((unsigned)(maxdepth.mm / scale)).arg(fract, 2, QChar('0')); } if (get_units()->length == units::FEET) { - str = QString::number(mm_to_feet(mm),'f',2); + str = QString::number(mm_to_feet(maxdepth.mm),'f',0); } return str; } @@ -350,8 +372,8 @@ QString DiveItem::displayDuration() const { int hrs, mins, secs; - secs = seconds % 60; - mins = seconds / 60; + secs = duration.seconds % 60; + mins = duration.seconds / 60; hrs = mins / 60; mins -= hrs * 60; @@ -364,6 +386,44 @@ QString DiveItem::displayDuration() const return displayTime; } +QString DiveItem::displayTemperature() const +{ + QString str; + + if (get_units()->temperature == units::CELSIUS) { + str = QString::number(mkelvin_to_C(temperature.mkelvin), 'f', 1); + } else { + str = QString::number(mkelvin_to_F(temperature.mkelvin), 'f', 1); + } + return str; +} + +QString DiveItem::displaySac() const +{ + QString str; + + if (get_units()->volume == units::LITER) { + str = QString::number(sac / 1000, 'f', 1); + } else { + str = QString::number(ml_to_cuft(sac), 'f', 2); + } + return str; +} + +QString DiveItem::displayWeight() const +{ + QString str; + + if (get_units()->weight == units::KG) { + int gr = totalweight.grams % 1000; + int kg = totalweight.grams / 1000; + str = QString("%1.%2").arg(kg).arg((unsigned)(gr + 500) / 100); + } else { + str = QString("%1").arg((unsigned)(grams_to_lbs(totalweight.grams) + 0.5)); + } + return str; +} + DiveTripModel::DiveTripModel(QObject *parent) : QAbstractItemModel(parent) { rootItem = new DiveItem; @@ -371,17 +431,20 @@ DiveTripModel::DiveTripModel(QObject *parent) : QAbstractItemModel(parent) struct dive *d; for_each_dive(i, d) { - struct tm tm; - char *buffer; - utc_mkdate(d->when, &tm); - buffer = get_dive_date_string(&tm); + weight_t tw = {.grams = total_weight(d)}; new DiveItem(d->number, - buffer, - d->duration.seconds, - d->maxdepth.mm, - d->location, - rootItem); - free(buffer); + d->when, + d->duration, + d->maxdepth, + d->rating, + d->watertemp, + tw, + d->suit, + d->sac, + d->otu, + d->maxcns, + d->location, + rootItem); } } @@ -406,12 +469,14 @@ QVariant DiveTripModel::data(const QModelIndex &index, int role) const QVariant retVal; if (role == Qt::TextAlignmentRole) { switch (index.column()) { - case DURATION: /* fall through */ - case DEPTH: - retVal = Qt::AlignRight; + case DATE: /* fall through */ + case SUIT: /* fall through */ + case LOCATION: + retVal = Qt::AlignLeft; break; default: - retVal = Qt::AlignLeft; + retVal = Qt::AlignRight; + break; } } if (role == Qt::DisplayRole) { @@ -422,13 +487,29 @@ QVariant DiveTripModel::data(const QModelIndex &index, int role) const case DATE: retVal = item->diveDateTime(); break; + case DEPTH: + retVal = item->displayDepth(); + break; case DURATION: retVal = item->displayDuration(); - //retVal = item->diveDuration(); break; - case DEPTH: - retVal = item->displayDepth(); - //retVal = item->diveDepth(); + case TEMPERATURE: + retVal = item->displayTemperature(); + break; + case TOTALWEIGHT: + retVal = item->displayWeight(); + break; + case SUIT: + retVal = item->diveSuit(); + break; + case SAC: + retVal = item->displaySac(); + break; + case OTU: + retVal = item->diveOtu(); + break; + case MAXCNS: + retVal = item->diveMaxcns(); break; case LOCATION: retVal = item->diveLocation(); -- cgit v1.2.3-70-g09d2 From 5d4d40df910b7be15c58f46768b8fe54b0b429f0 Mon Sep 17 00:00:00 2001 From: Henrik Brautaset Aronsen Date: Thu, 25 Apr 2013 09:50:01 +0200 Subject: Simplify DiveItem The DiveItem constructor had 13 variables. By passing it the full dive we reduce that to 2. [Dirk Hohndel: changed to use "struct dive *" instead of just "dive *"] Signed-off-by: Henrik Brautaset Aronsen Signed-off-by: Dirk Hohndel --- qt-ui/models.cpp | 47 ++++++++++++++++++++++------------------------- 1 file changed, 22 insertions(+), 25 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index cf6490051..a725d0d61 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -63,7 +63,7 @@ QVariant CylindersModel::data(const QModelIndex& index, int role) const return ret; } - dive *d = get_dive(selected_dive); + struct dive *d = get_dive(selected_dive); cylinder_t& cyl = d->cylinder[index.row()]; if (role == Qt::DisplayRole) { @@ -297,8 +297,9 @@ public: explicit DiveItem(): number(0), when(), duration(), maxdepth(), rating(0), temperature(), totalweight(), suit(QString()), sac(0), otu(0), maxcns(0), location(QString()) { parentItem = 0; } - explicit DiveItem(int, timestamp_t, duration_t, depth_t, int, temperature_t, - weight_t, QString, int, int, int, QString, DiveItem *parent = 0); + + explicit DiveItem(struct dive *d, DiveItem *parent = 0); + ~DiveItem() { qDeleteAll(childlist); } int diveNumber() const { return number; } @@ -341,15 +342,24 @@ private: QList childlist; }; -DiveItem::DiveItem(int num, timestamp_t when, duration_t duration, depth_t maxdepth, int rating, temperature_t temp, - weight_t weight, QString su, int sac, int otu, int maxcns, QString loc, DiveItem *p): - number(num), rating(rating), suit(su), sac(sac), otu(otu), maxcns(maxcns), location(loc), parentItem(p) +DiveItem::DiveItem(struct dive *d, DiveItem *p): + number(d->number), + rating(d->rating), + suit(d->suit), + sac(d->sac), + otu(d->otu), + maxcns(d->maxcns), + location(d->location), + parentItem(p) { - this->when = when; - this->duration = duration; - this->maxdepth = maxdepth; - this->temperature = temp; - this->totalweight = weight; + this->when = d->when; + this->duration = d->duration; + this->maxdepth = d->maxdepth; + this->temperature = d->watertemp; + + weight_t tw = { total_weight(d) }; + this->totalweight = tw; + if (parentItem) parentItem->addChild(this); } @@ -431,20 +441,7 @@ DiveTripModel::DiveTripModel(QObject *parent) : QAbstractItemModel(parent) struct dive *d; for_each_dive(i, d) { - weight_t tw = {.grams = total_weight(d)}; - new DiveItem(d->number, - d->when, - d->duration, - d->maxdepth, - d->rating, - d->watertemp, - tw, - d->suit, - d->sac, - d->otu, - d->maxcns, - d->location, - rootItem); + new DiveItem(d, rootItem); } } -- cgit v1.2.3-70-g09d2 From 8a884d2cf7d7680bc2c94000954684e3b95d0c12 Mon Sep 17 00:00:00 2001 From: Henrik Brautaset Aronsen Date: Thu, 25 Apr 2013 16:04:41 +0200 Subject: Remove useless members of DiveItem Just use the dive struct directly. Suggested-by: Dirk Hohndel Signed-off-by: Henrik Brautaset Aronsen Signed-off-by: Dirk Hohndel --- qt-ui/models.cpp | 83 +++++++++++++++++++++----------------------------------- 1 file changed, 31 insertions(+), 52 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index a725d0d61..7894aa279 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -294,29 +294,33 @@ void TankInfoModel::update() class DiveItem { public: - explicit DiveItem(): number(0), when(), duration(), maxdepth(), rating(0), - temperature(), totalweight(), suit(QString()), sac(0), - otu(0), maxcns(0), location(QString()) { parentItem = 0; } + explicit DiveItem(): dive() { parentItem = 0; } explicit DiveItem(struct dive *d, DiveItem *parent = 0); ~DiveItem() { qDeleteAll(childlist); } - int diveNumber() const { return number; } - const QString diveDateTime() const { return QString::fromUtf8(get_dive_date_string(when)); } - int diveDuration() const { return duration.seconds; } - int diveDepth() const { return maxdepth.mm; } - int diveSac() const { return sac; } - int diveOtu() const { return otu; } - int diveMaxcns() const { return maxcns; } - int diveWeight() const { return totalweight.grams; } + int diveNumber() const { return dive->number; } + const QString diveDateTime() const { return QString::fromUtf8(get_dive_date_string(dive->when)); } + int diveDuration() const { return dive->duration.seconds; } + int diveDepth() const { return dive->maxdepth.mm; } + int diveSac() const { return dive->sac; } + int diveOtu() const { return dive->otu; } + int diveMaxcns() const { return dive->maxcns; } + + int diveWeight() const + { + weight_t tw = { total_weight(dive) }; + return tw.grams; + } + QString displayDuration() const; QString displayDepth() const; QString displayTemperature() const; QString displayWeight() const; QString displaySac() const; - const QString& diveLocation() const { return location; } - const QString& diveSuit() const { return suit; } + const QString diveLocation() const { return dive->location; } + const QString diveSuit() const { return dive->suit; } DiveItem *parent() const { return parentItem; } const QList& children() const { return childlist; } @@ -326,40 +330,15 @@ public: } /* parent = self */ private: - int number; - timestamp_t when; - duration_t duration; - depth_t maxdepth; - int rating; - temperature_t temperature; - weight_t totalweight; - QString suit; - int sac; - int otu; - int maxcns; - QString location; + struct dive *dive; DiveItem *parentItem; QList childlist; }; DiveItem::DiveItem(struct dive *d, DiveItem *p): - number(d->number), - rating(d->rating), - suit(d->suit), - sac(d->sac), - otu(d->otu), - maxcns(d->maxcns), - location(d->location), + dive(d), parentItem(p) { - this->when = d->when; - this->duration = d->duration; - this->maxdepth = d->maxdepth; - this->temperature = d->watertemp; - - weight_t tw = { total_weight(d) }; - this->totalweight = tw; - if (parentItem) parentItem->addChild(this); } @@ -369,11 +348,11 @@ QString DiveItem::displayDepth() const const int scale = 1000; QString fract, str; if (get_units()->length == units::METERS) { - fract = QString::number((unsigned)(maxdepth.mm % scale) / 10); - str = QString("%1.%2").arg((unsigned)(maxdepth.mm / scale)).arg(fract, 2, QChar('0')); + fract = QString::number((unsigned)(dive->maxdepth.mm % scale) / 10); + str = QString("%1.%2").arg((unsigned)(dive->maxdepth.mm / scale)).arg(fract, 2, QChar('0')); } if (get_units()->length == units::FEET) { - str = QString::number(mm_to_feet(maxdepth.mm),'f',0); + str = QString::number(mm_to_feet(dive->maxdepth.mm),'f',0); } return str; } @@ -382,8 +361,8 @@ QString DiveItem::displayDuration() const { int hrs, mins, secs; - secs = duration.seconds % 60; - mins = duration.seconds / 60; + secs = dive->duration.seconds % 60; + mins = dive->duration.seconds / 60; hrs = mins / 60; mins -= hrs * 60; @@ -401,9 +380,9 @@ QString DiveItem::displayTemperature() const QString str; if (get_units()->temperature == units::CELSIUS) { - str = QString::number(mkelvin_to_C(temperature.mkelvin), 'f', 1); + str = QString::number(mkelvin_to_C(dive->watertemp.mkelvin), 'f', 1); } else { - str = QString::number(mkelvin_to_F(temperature.mkelvin), 'f', 1); + str = QString::number(mkelvin_to_F(dive->watertemp.mkelvin), 'f', 1); } return str; } @@ -413,9 +392,9 @@ QString DiveItem::displaySac() const QString str; if (get_units()->volume == units::LITER) { - str = QString::number(sac / 1000, 'f', 1); + str = QString::number(dive->sac / 1000, 'f', 1); } else { - str = QString::number(ml_to_cuft(sac), 'f', 2); + str = QString::number(ml_to_cuft(dive->sac), 'f', 2); } return str; } @@ -425,11 +404,11 @@ QString DiveItem::displayWeight() const QString str; if (get_units()->weight == units::KG) { - int gr = totalweight.grams % 1000; - int kg = totalweight.grams / 1000; + int gr = diveWeight() % 1000; + int kg = diveWeight() / 1000; str = QString("%1.%2").arg(kg).arg((unsigned)(gr + 500) / 100); } else { - str = QString("%1").arg((unsigned)(grams_to_lbs(totalweight.grams) + 0.5)); + str = QString("%1").arg((unsigned)(grams_to_lbs(diveWeight()) + 0.5)); } return str; } -- cgit v1.2.3-70-g09d2 From 98027be1c1895013bfc3c4071c5d61d10fb70092 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Thu, 25 Apr 2013 08:38:58 -0700 Subject: Minor cleanup of constructors and one accessor for DiveItem Suggested-by: Thiago Macieira Signed-off-by: Dirk Hohndel --- qt-ui/models.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 7894aa279..2fbfa9f88 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -294,9 +294,9 @@ void TankInfoModel::update() class DiveItem { public: - explicit DiveItem(): dive() { parentItem = 0; } + DiveItem(): dive(NULL), parentItem(NULL) {} - explicit DiveItem(struct dive *d, DiveItem *parent = 0); + DiveItem(struct dive *d, DiveItem *parent = NULL); ~DiveItem() { qDeleteAll(childlist); } @@ -319,7 +319,7 @@ public: QString displayTemperature() const; QString displayWeight() const; QString displaySac() const; - const QString diveLocation() const { return dive->location; } + const QString diveLocation() const { return QString::fromUtf8(dive->location); } const QString diveSuit() const { return dive->suit; } DiveItem *parent() const { return parentItem; } const QList& children() const { return childlist; } -- cgit v1.2.3-70-g09d2 From 1d0d42f861fc3a658eb22b99ba58616d716e095e Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Thu, 25 Apr 2013 21:10:05 -0700 Subject: Remove the explicit UTF-8 conversions Thanks to commit bdbfdcdfa0fb ('Ask Qt 4 to use the UTF-8 codec as the "codec for C strings"') we no longer need the explicit UTF-8 conversion when creating QStrings from char *. Suggested-by: Thiago Macieira Signed-off-by: Dirk Hohndel --- qt-gui.cpp | 4 ++-- qt-ui/models.cpp | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/qt-gui.cpp b/qt-gui.cpp index d13c3c449..7853cc4c0 100644 --- a/qt-gui.cpp +++ b/qt-gui.cpp @@ -60,7 +60,7 @@ Translator::Translator(QObject *parent): QString Translator::translate(const char *context, const char *sourceText, const char *disambiguation) const { - return QString::fromUtf8(gettext(sourceText)); + return gettext(sourceText); } static const GdkPixdata subsurface_icon_pixbuf = {}; @@ -1795,7 +1795,7 @@ void MainWindow::setCurrentFileName(const QString &fileName) void MainWindow::on_actionOpen_triggered() { - QString defaultFileName = QString::fromUtf8(prefs.default_filename); + QString defaultFileName = prefs.default_filename; QFileInfo fileInfo(defaultFileName); QFileDialog dialog(this, tr("Open File"), fileInfo.path()); diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 2fbfa9f88..5f803766f 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -301,7 +301,7 @@ public: ~DiveItem() { qDeleteAll(childlist); } int diveNumber() const { return dive->number; } - const QString diveDateTime() const { return QString::fromUtf8(get_dive_date_string(dive->when)); } + const QString diveDateTime() const { return get_dive_date_string(dive->when); } int diveDuration() const { return dive->duration.seconds; } int diveDepth() const { return dive->maxdepth.mm; } int diveSac() const { return dive->sac; } @@ -319,7 +319,7 @@ public: QString displayTemperature() const; QString displayWeight() const; QString displaySac() const; - const QString diveLocation() const { return QString::fromUtf8(dive->location); } + const QString diveLocation() const { return dive->location; } const QString diveSuit() const { return dive->suit; } DiveItem *parent() const { return parentItem; } const QList& children() const { return childlist; } @@ -511,7 +511,7 @@ QVariant DiveTripModel::headerData(int section, Qt::Orientation orientation, int ret = tr("Date"); break; case RATING: - ret = QString::fromUtf8(UTF8_BLACKSTAR); + ret = UTF8_BLACKSTAR; break; case DEPTH: if (get_units()->length == units::METERS) @@ -524,9 +524,9 @@ QVariant DiveTripModel::headerData(int section, Qt::Orientation orientation, int break; case TEMPERATURE: if (get_units()->temperature == units::CELSIUS) - ret = QString("%1%2").arg(QString::fromUtf8(UTF8_DEGREE)).arg("C"); + ret = QString("%1%2").arg(UTF8_DEGREE).arg("C"); else - ret = QString("%1%2").arg(QString::fromUtf8(UTF8_DEGREE)).arg("F"); + ret = QString("%1%2").arg(UTF8_DEGREE).arg("F"); break; case TOTALWEIGHT: if (get_units()->weight == units::KG) @@ -541,7 +541,7 @@ QVariant DiveTripModel::headerData(int section, Qt::Orientation orientation, int ret = tr("Cyl"); break; case NITROX: - ret = QString("O%1%").arg(QString::fromUtf8(UTF8_SUBSCRIPT_2)); + ret = QString("O%1%").arg(UTF8_SUBSCRIPT_2); break; case SAC: ret = tr("SAC"); -- cgit v1.2.3-70-g09d2 From 2f4d6bbe535a195046b4746fd3a771087ee4a6c4 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Sat, 27 Apr 2013 12:27:27 -0300 Subject: Added support for showing the Stars on the DiveTable For the stars on the dive table I had to rework a bit my StarRating widget, because it used a pixmap for each widget that were created. Not it uses only 2 pixmaps: the active and inactive ones. A new file was created named modeldelegates(h, cpp) that should hold all delegates of the models. For the GTK / C folks, a 'Delegate' ia s way to bypass the default behavior of the view that's displaying the data. I also added the code to display the stars if no delegate is set ( good for debugging. ) Signed-off-by: Tomaz Canabrava --- Makefile | 2 ++ qt-ui/divelistview.cpp | 2 ++ qt-ui/mainwindow.cpp | 3 ++- qt-ui/modeldelegates.cpp | 36 +++++++++++++++++++++++++ qt-ui/modeldelegates.h | 12 +++++++++ qt-ui/models.cpp | 19 +++++++++---- qt-ui/models.h | 5 ++-- qt-ui/starwidget.cpp | 70 +++++++++++++++++++++++------------------------- qt-ui/starwidget.h | 15 ++++------- 9 files changed, 110 insertions(+), 54 deletions(-) create mode 100644 qt-ui/modeldelegates.cpp create mode 100644 qt-ui/modeldelegates.h (limited to 'qt-ui/models.cpp') diff --git a/Makefile b/Makefile index 6ebba4dd5..aac1759bc 100644 --- a/Makefile +++ b/Makefile @@ -37,6 +37,7 @@ HEADERS = \ qt-ui/models.h \ qt-ui/plotareascene.h \ qt-ui/starwidget.h \ + qt-ui/modeldelegates.h \ SOURCES = \ @@ -75,6 +76,7 @@ SOURCES = \ qt-ui/models.cpp \ qt-ui/plotareascene.cpp \ qt-ui/starwidget.cpp \ + qt-ui/modeldelegates.cpp \ $(RESFILE) diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp index 676d7c463..e8a3d2311 100644 --- a/qt-ui/divelistview.cpp +++ b/qt-ui/divelistview.cpp @@ -5,6 +5,8 @@ * */ #include "divelistview.h" +#include "models.h" +#include "modeldelegates.h" DiveListView::DiveListView(QWidget *parent) : QTreeView(parent) { diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp index 8cdc60193..4c4143e98 100644 --- a/qt-ui/mainwindow.cpp +++ b/qt-ui/mainwindow.cpp @@ -20,7 +20,7 @@ #include "../dive.h" #include "../divelist.h" #include "../pref.h" - +#include "modeldelegates.h" MainWindow::MainWindow() : ui(new Ui::MainWindow()), model(new DiveTripModel(this)), @@ -69,6 +69,7 @@ void MainWindow::on_actionOpen_triggered() model->deleteLater(); model = new DiveTripModel(this); sortModel->setSourceModel(model); + ui->ListWidget->setItemDelegateForColumn(DiveTripModel::RATING, new StarWidgetsDelegate()); } void MainWindow::on_actionSave_triggered() diff --git a/qt-ui/modeldelegates.cpp b/qt-ui/modeldelegates.cpp new file mode 100644 index 000000000..1bbf1061b --- /dev/null +++ b/qt-ui/modeldelegates.cpp @@ -0,0 +1,36 @@ +#include "modeldelegates.h" +#include "../dive.h" +#include "../divelist.h" +#include "starwidget.h" +#include "models.h" + +#include +#include + +void StarWidgetsDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const +{ + if (!index.isValid()){ + return; + } + + int rating = index.model()->data(index, DiveTripModel::DelegatesRole).toInt(); + + if (option.state & QStyle::State_Selected) + painter->fillRect(option.rect, option.palette.highlight()); + + painter->save(); + painter->setRenderHint(QPainter::Antialiasing, true); + + for(int i = 0; i < rating; i++) + painter->drawPixmap(option.rect.x() + i * IMG_SIZE + SPACING, option.rect.y(), StarWidget::starActive()); + + for(int i = rating; i < TOTALSTARS; i++) + painter->drawPixmap(option.rect.x() + i * IMG_SIZE + SPACING, option.rect.y(), StarWidget::starInactive()); + + painter->restore(); +} + +QSize StarWidgetsDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const +{ + return QSize(IMG_SIZE * TOTALSTARS + SPACING * (TOTALSTARS-1), IMG_SIZE); +} diff --git a/qt-ui/modeldelegates.h b/qt-ui/modeldelegates.h new file mode 100644 index 000000000..eacbb5a1e --- /dev/null +++ b/qt-ui/modeldelegates.h @@ -0,0 +1,12 @@ +#ifndef MODELDELEGATES_H +#define MODELDELEGATES_H + +#include + +class StarWidgetsDelegate : public QAbstractItemDelegate { + Q_OBJECT +public: + virtual void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; + virtual QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const; +}; +#endif diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 5f803766f..0944fe3e3 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -5,9 +5,6 @@ * */ #include "models.h" -#include "../dive.h" -#include "../divelist.h" - #include extern struct tank_info tank_info[100]; @@ -284,6 +281,7 @@ void TankInfoModel::update() } } + /*! A DiveItem for use with a DiveTripModel * * A simple class which wraps basic stats for a dive (e.g. duration, depth) and @@ -314,6 +312,8 @@ public: return tw.grams; } + int diveRating() const { return dive->rating; } + QString displayDuration() const; QString displayDepth() const; QString displayTemperature() const; @@ -335,6 +335,7 @@ private: QList childlist; }; + DiveItem::DiveItem(struct dive *d, DiveItem *p): dive(d), parentItem(p) @@ -490,6 +491,16 @@ QVariant DiveTripModel::data(const QModelIndex &index, int role) const case LOCATION: retVal = item->diveLocation(); break; + case RATING: + retVal = item->diveRating(); + break; + } + } + if (role == DelegatesRole){ + switch(index.column()){ + case RATING: + retVal = item->diveRating(); + break; } } return retVal; @@ -569,8 +580,6 @@ int DiveTripModel::rowCount(const QModelIndex &parent) const return item ? item->children().count() : 0; } - - int DiveTripModel::columnCount(const QModelIndex &parent) const { return parent.isValid() && parent.column() != 0 ? 0 : COLUMNS; diff --git a/qt-ui/models.h b/qt-ui/models.h index d64faf0bd..f4d9c8d3b 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -9,7 +9,7 @@ #include #include "../dive.h" - +#include "../divelist.h" /* Encapsulates the tank_info global variable * to show on Qt`s Model View System.*/ class TankInfoModel : public QAbstractTableModel { @@ -74,11 +74,12 @@ private: /*! An AbstractItemModel for recording dive trip information such as a list of dives. * */ -class DiveItem; // Represents a single item on the model, implemented in the .cpp since it's private for this class. +class DiveItem; class DiveTripModel : public QAbstractItemModel { public: enum Column {NR, DATE, RATING, DEPTH, DURATION, TEMPERATURE, TOTALWEIGHT, SUIT, CYLINDER, NITROX, SAC, OTU, MAXCNS, LOCATION, COLUMNS }; + enum { DelegatesRole = Qt::UserRole }; DiveTripModel(QObject *parent = 0); diff --git a/qt-ui/starwidget.cpp b/qt-ui/starwidget.cpp index 866fb5834..4d1fa066c 100644 --- a/qt-ui/starwidget.cpp +++ b/qt-ui/starwidget.cpp @@ -5,32 +5,29 @@ #include #include -int StarWidget::currentStars() const -{ - return current; -} +QPixmap* StarWidget::activeStar = 0; +QPixmap* StarWidget::inactiveStar = 0; -void StarWidget::enableHalfStars(bool enabled) +QPixmap StarWidget::starActive() { - halfStar = enabled; - update(); + return (*activeStar); } -bool StarWidget::halfStarsEnabled() const +QPixmap StarWidget::starInactive() { - return halfStar; + return (*inactiveStar); } -int StarWidget::maxStars() const +int StarWidget::currentStars() const { - return stars; + return current; } void StarWidget::mouseReleaseEvent(QMouseEvent* event) { int starClicked = event->pos().x() / IMG_SIZE + 1; - if (starClicked > stars) - starClicked = stars; + if (starClicked > TOTALSTARS) + starClicked = TOTALSTARS; if (current == starClicked) current -= 1; @@ -45,10 +42,10 @@ void StarWidget::paintEvent(QPaintEvent* event) QPainter p(this); for(int i = 0; i < current; i++) - p.drawPixmap(i * IMG_SIZE + SPACING, 0, activeStar); + p.drawPixmap(i * IMG_SIZE + SPACING, 0, starActive()); - for(int i = current; i < stars; i++) - p.drawPixmap(i * IMG_SIZE + SPACING, 0, inactiveStar); + for(int i = current; i < TOTALSTARS; i++) + p.drawPixmap(i * IMG_SIZE + SPACING, 0, starInactive()); } void StarWidget::setCurrentStars(int value) @@ -58,27 +55,25 @@ void StarWidget::setCurrentStars(int value) Q_EMIT valueChanged(current); } -void StarWidget::setMaximumStars(int maximum) -{ - stars = maximum; - update(); -} - StarWidget::StarWidget(QWidget* parent, Qt::WindowFlags f): QWidget(parent, f), - stars(5), - current(0), - halfStar(false) + current(0) { - QSvgRenderer render(QString(":star")); - QPixmap renderedStar(IMG_SIZE, IMG_SIZE); + if(!activeStar){ + activeStar = new QPixmap(); + QSvgRenderer render(QString(":star")); + QPixmap renderedStar(IMG_SIZE, IMG_SIZE); - renderedStar.fill(Qt::transparent); - QPainter painter(&renderedStar); + renderedStar.fill(Qt::transparent); + QPainter painter(&renderedStar); - render.render(&painter, QRectF(0, 0, IMG_SIZE, IMG_SIZE)); - activeStar = renderedStar; - inactiveStar = grayImage(&renderedStar); + render.render(&painter, QRectF(0, 0, IMG_SIZE, IMG_SIZE)); + (*activeStar) = renderedStar; + } + if(!inactiveStar){ + inactiveStar = new QPixmap(); + (*inactiveStar) = grayImage(activeStar); + } } QPixmap StarWidget::grayImage(QPixmap* coloredImg) @@ -86,17 +81,20 @@ QPixmap StarWidget::grayImage(QPixmap* coloredImg) QImage img = coloredImg->toImage(); for (int i = 0; i < img.width(); ++i) { for (int j = 0; j < img.height(); ++j) { - QRgb col = img.pixel(i, j); - if (!col) + QRgb rgb = img.pixel(i, j); + if (!rgb) continue; - int gray = QColor(Qt::darkGray).rgb(); + + QColor c(rgb); + int gray = (c.red() + c.green() + c.blue()) / 3; img.setPixel(i, j, qRgb(gray, gray, gray)); } } + return QPixmap::fromImage(img); } QSize StarWidget::sizeHint() const { - return QSize(IMG_SIZE * stars + SPACING * (stars-1), IMG_SIZE); + return QSize(IMG_SIZE * TOTALSTARS + SPACING * (TOTALSTARS-1), IMG_SIZE); } diff --git a/qt-ui/starwidget.h b/qt-ui/starwidget.h index cdbb3ab5c..d92be5a98 100644 --- a/qt-ui/starwidget.h +++ b/qt-ui/starwidget.h @@ -3,39 +3,34 @@ #include +enum StarConfig {SPACING = 2, IMG_SIZE = 16, TOTALSTARS = 5}; class StarWidget : public QWidget { Q_OBJECT public: explicit StarWidget(QWidget* parent = 0, Qt::WindowFlags f = 0); - - int maxStars() const; int currentStars() const; - bool halfStarsEnabled() const; /*reimp*/ QSize sizeHint() const; - enum {SPACING = 2, IMG_SIZE = 16}; + static QPixmap starActive(); + static QPixmap starInactive(); Q_SIGNALS: void valueChanged(int stars); public Q_SLOTS: void setCurrentStars(int value); - void setMaximumStars(int maximum); - void enableHalfStars(bool enabled); protected: /*reimp*/ void mouseReleaseEvent(QMouseEvent* ); /*reimp*/ void paintEvent(QPaintEvent* ); private: - int stars; int current; - bool halfStar; - QPixmap activeStar; - QPixmap inactiveStar; + static QPixmap* activeStar; + static QPixmap* inactiveStar; QPixmap grayImage(QPixmap *coloredImg); }; -- cgit v1.2.3-70-g09d2 From 0be521bb25cf6210ad47e42eb7a8eb7638c32442 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Sun, 28 Apr 2013 08:45:22 -0300 Subject: Fixed loading the stars when opening with file as argv. minor cleanup Signed-off-by: Tomaz Canabrava --- qt-ui/divelistview.cpp | 1 + qt-ui/mainwindow.cpp | 2 +- qt-ui/modeldelegates.cpp | 3 ++- qt-ui/models.cpp | 7 ------- qt-ui/models.h | 1 - 5 files changed, 4 insertions(+), 10 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp index e8a3d2311..fb19a7060 100644 --- a/qt-ui/divelistview.cpp +++ b/qt-ui/divelistview.cpp @@ -11,4 +11,5 @@ DiveListView::DiveListView(QWidget *parent) : QTreeView(parent) { setUniformRowHeights(true); + setItemDelegateForColumn(DiveTripModel::RATING, new StarWidgetsDelegate()); } diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp index 4c4143e98..f4538db22 100644 --- a/qt-ui/mainwindow.cpp +++ b/qt-ui/mainwindow.cpp @@ -69,7 +69,7 @@ void MainWindow::on_actionOpen_triggered() model->deleteLater(); model = new DiveTripModel(this); sortModel->setSourceModel(model); - ui->ListWidget->setItemDelegateForColumn(DiveTripModel::RATING, new StarWidgetsDelegate()); + ui->ListWidget->sortByColumn(0, Qt::DescendingOrder); } void MainWindow::on_actionSave_triggered() diff --git a/qt-ui/modeldelegates.cpp b/qt-ui/modeldelegates.cpp index 1bbf1061b..1ac2f46c6 100644 --- a/qt-ui/modeldelegates.cpp +++ b/qt-ui/modeldelegates.cpp @@ -6,6 +6,7 @@ #include #include +#include void StarWidgetsDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { @@ -13,7 +14,7 @@ void StarWidgetsDelegate::paint(QPainter* painter, const QStyleOptionViewItem& o return; } - int rating = index.model()->data(index, DiveTripModel::DelegatesRole).toInt(); + int rating = index.model()->data(index, Qt::DisplayRole).toInt(); if (option.state & QStyle::State_Selected) painter->fillRect(option.rect, option.palette.highlight()); diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 0944fe3e3..23eea3e48 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -496,13 +496,6 @@ QVariant DiveTripModel::data(const QModelIndex &index, int role) const break; } } - if (role == DelegatesRole){ - switch(index.column()){ - case RATING: - retVal = item->diveRating(); - break; - } - } return retVal; } diff --git a/qt-ui/models.h b/qt-ui/models.h index f4d9c8d3b..9e4666dc7 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -79,7 +79,6 @@ class DiveTripModel : public QAbstractItemModel { public: enum Column {NR, DATE, RATING, DEPTH, DURATION, TEMPERATURE, TOTALWEIGHT, SUIT, CYLINDER, NITROX, SAC, OTU, MAXCNS, LOCATION, COLUMNS }; - enum { DelegatesRole = Qt::UserRole }; DiveTripModel(QObject *parent = 0); -- cgit v1.2.3-70-g09d2 From 730e055e5aacd2f917be9d86140773853670e850 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Wed, 1 May 2013 09:04:14 -0700 Subject: Use the existing current_dive macro in Qt code Replicating this with the currentDive member seems to make no sense. Signed-off-by: Dirk Hohndel --- qt-ui/models.cpp | 39 ++++++++++++++++++++------------------- qt-ui/models.h | 7 +++---- 2 files changed, 23 insertions(+), 23 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 23eea3e48..d26b52ec9 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -59,9 +59,7 @@ QVariant CylindersModel::data(const QModelIndex& index, int role) const if (!index.isValid() || index.row() >= MAX_CYLINDERS) { return ret; } - - struct dive *d = get_dive(selected_dive); - cylinder_t& cyl = d->cylinder[index.row()]; + cylinder_t& cyl = current_dive->cylinder[index.row()]; if (role == Qt::DisplayRole) { switch(index.column()) { @@ -93,57 +91,60 @@ QVariant CylindersModel::data(const QModelIndex& index, int role) const int CylindersModel::rowCount(const QModelIndex& parent) const { - return usedRows[currentDive]; + return usedRows[current_dive]; } void CylindersModel::add(cylinder_t* cyl) { - if (usedRows[currentDive] >= MAX_CYLINDERS) { + if (usedRows[current_dive] >= MAX_CYLINDERS) { free(cyl); } - int row = usedRows[currentDive]; + int row = usedRows[current_dive]; - cylinder_t& cylinder = currentDive->cylinder[row]; + cylinder_t& cylinder = current_dive->cylinder[row]; cylinder.end.mbar = cyl->end.mbar; cylinder.start.mbar = cyl->start.mbar; beginInsertRows(QModelIndex(), row, row); - usedRows[currentDive]++; + usedRows[current_dive]++; endInsertRows(); } void CylindersModel::update() { - if (usedRows[currentDive] > 0) { - beginRemoveRows(QModelIndex(), 0, usedRows[currentDive]-1); + if (usedRows[current_dive] > 0) { + beginRemoveRows(QModelIndex(), 0, usedRows[current_dive]-1); endRemoveRows(); } - - currentDive = get_dive(selected_dive); - if (usedRows[currentDive] > 0) { - beginInsertRows(QModelIndex(), 0, usedRows[currentDive]-1); + if (usedRows[current_dive] > 0) { + beginInsertRows(QModelIndex(), 0, usedRows[current_dive]-1); endInsertRows(); } } void CylindersModel::clear() { - if (usedRows[currentDive] > 0) { - beginRemoveRows(QModelIndex(), 0, usedRows[currentDive]-1); - usedRows[currentDive] = 0; + if (usedRows[current_dive] > 0) { + beginRemoveRows(QModelIndex(), 0, usedRows[current_dive]-1); + usedRows[current_dive] = 0; endRemoveRows(); } } void WeightModel::clear() { + if (usedRows[current_dive] > 0) { + beginRemoveRows(QModelIndex(), 0, usedRows[current_dive]-1); + usedRows[current_dive] = 0; + endRemoveRows(); + } } int WeightModel::columnCount(const QModelIndex& parent) const { - return 0; + return 2; } QVariant WeightModel::data(const QModelIndex& index, int role) const @@ -153,7 +154,7 @@ QVariant WeightModel::data(const QModelIndex& index, int role) const int WeightModel::rowCount(const QModelIndex& parent) const { - return rows; + return usedRows[current_dive]; } QVariant WeightModel::headerData(int section, Qt::Orientation orientation, int role) const diff --git a/qt-ui/models.h b/qt-ui/models.h index 9e4666dc7..b6bcdec78 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -47,12 +47,10 @@ public: void clear(); void update(); private: - dive *currentDive; - /* Since the dive doesn`t stores the number of cylinders that * it has ( max 8 ) and since I don`t want to make a * model-for-each-dive, let`s hack this here instead. */ - QMap usedRows; + QMap usedRows; }; /* Encapsulation of the Weight Model, that represents @@ -68,7 +66,8 @@ class WeightModel : public QAbstractTableModel { void clear(); void update(); private: - int rows; + /* Remember the number of rows in a dive */ + QMap usedRows; }; /*! An AbstractItemModel for recording dive trip information such as a list of dives. -- cgit v1.2.3-70-g09d2 From 5c2ce0ac200cbf4b37a9765148b78d3091b5cd9f Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Wed, 1 May 2013 10:11:46 -0700 Subject: Add data and add functions for WeightModel Signed-off-by: Dirk Hohndel --- qt-ui/models.cpp | 40 ++++++++++++++++++++++++++++++++++++++-- qt-ui/models.h | 2 +- 2 files changed, 39 insertions(+), 3 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index d26b52ec9..4583c36cd 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -149,7 +149,29 @@ int WeightModel::columnCount(const QModelIndex& parent) const QVariant WeightModel::data(const QModelIndex& index, int role) const { - return QVariant(); + QVariant ret; + if (!index.isValid() || index.row() >= MAX_WEIGHTSYSTEMS) { + return ret; + } + weightsystem_t *ws = ¤t_dive->weightsystem[index.row()]; + + if (role == Qt::DisplayRole) { + switch(index.column()) { + case TYPE: + ret = QString(ws->description); + break; + case WEIGHT: + if (get_units()->weight == units::KG) { + int gr = ws->weight.grams % 1000; + int kg = ws->weight.grams / 1000; + ret = QString("%1.%2").arg(kg).arg((unsigned)(gr + 500) / 100); + } else { + ret = QString("%1").arg((unsigned)(grams_to_lbs(ws->weight.grams) + 0.5)); + } + break; + } + } + return ret; } int WeightModel::rowCount(const QModelIndex& parent) const @@ -175,8 +197,22 @@ QVariant WeightModel::headerData(int section, Qt::Orientation orientation, int r return ret; } -void WeightModel::add(weight_t* weight) +void WeightModel::add(weightsystem_t* weight) { + if (usedRows[current_dive] >= MAX_WEIGHTSYSTEMS) { + free(weight); + } + + int row = usedRows[current_dive]; + + weightsystem_t *ws = ¤t_dive->weightsystem[row]; + + ws->description = weight->description; + ws->weight.grams = weight->weight.grams; + + beginInsertRows(QModelIndex(), row, row); + usedRows[current_dive]++; + endInsertRows(); } void WeightModel::update() diff --git a/qt-ui/models.h b/qt-ui/models.h index b6bcdec78..46da9e51b 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -62,7 +62,7 @@ class WeightModel : public QAbstractTableModel { /*reimp*/ QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const; /*reimp*/ int rowCount(const QModelIndex& parent = QModelIndex()) const; - void add(weight_t *weight); + void add(weightsystem_t *weight); void clear(); void update(); private: -- cgit v1.2.3-70-g09d2 From 04e59a0e1cdb32c5091fc4bc0d692f00821ab849 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Wed, 1 May 2013 14:30:34 -0700 Subject: Hook up adding a weightsystem Signed-off-by: Dirk Hohndel --- qt-ui/maintab.cpp | 17 +++++++++++++++++ qt-ui/maintab.h | 3 +++ qt-ui/maintab.ui | 6 +++--- qt-ui/models.cpp | 16 +++++++++------- qt-ui/models.h | 2 ++ 5 files changed, 34 insertions(+), 10 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/qt-ui/maintab.cpp b/qt-ui/maintab.cpp index 7d60db5c2..f8c69ed9c 100644 --- a/qt-ui/maintab.cpp +++ b/qt-ui/maintab.cpp @@ -83,6 +83,23 @@ void MainTab::on_delCylinder_clicked() { } +void MainTab::on_addWeight_clicked() +{ + /* this needs a dialog - right now we just fill in a dummy */ + weightsystem_t *newWeightsystem = (weightsystem_t *) malloc(sizeof(weightsystem_t)); + newWeightsystem->description = "Just testing"; + newWeightsystem->weight.grams = 15000; + weightModel->add(newWeightsystem); +} + +void MainTab::on_editWeight_clicked() +{ +} + +void MainTab::on_delWeight_clicked() +{ +} + void MainTab::reload() { cylindersModel->update(); diff --git a/qt-ui/maintab.h b/qt-ui/maintab.h index 44815fafc..cf83e0dfe 100644 --- a/qt-ui/maintab.h +++ b/qt-ui/maintab.h @@ -31,6 +31,9 @@ public Q_SLOTS: void on_addCylinder_clicked(); void on_editCylinder_clicked(); void on_delCylinder_clicked(); + void on_addWeight_clicked(); + void on_editWeight_clicked(); + void on_delWeight_clicked(); private: Ui::MainTab *ui; diff --git a/qt-ui/maintab.ui b/qt-ui/maintab.ui index 7edbf5837..a99b0aed7 100644 --- a/qt-ui/maintab.ui +++ b/qt-ui/maintab.ui @@ -152,14 +152,14 @@ - + Edit - + Add @@ -179,7 +179,7 @@ - + Delete diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 4583c36cd..6756002e2 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -186,13 +186,15 @@ QVariant WeightModel::headerData(int section, Qt::Orientation orientation, int r return ret; } - switch(section) { - case TYPE: - ret = tr("Type"); - break; - case WEIGHT: - ret = tr("Weight"); - break; + if (role == Qt::DisplayRole) { + switch(section) { + case TYPE: + ret = tr("Type"); + break; + case WEIGHT: + ret = tr("Weight"); + break; + } } return ret; } diff --git a/qt-ui/models.h b/qt-ui/models.h index 46da9e51b..9a4602f18 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -56,6 +56,8 @@ private: /* Encapsulation of the Weight Model, that represents * the current weights on a dive. */ class WeightModel : public QAbstractTableModel { +Q_OBJECT +public: enum Column {TYPE, WEIGHT}; /*reimp*/ QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; /*reimp*/ int columnCount(const QModelIndex& parent = QModelIndex()) const; -- cgit v1.2.3-70-g09d2 From 482bea84c2f218f515b3b16556197379623a8028 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Wed, 1 May 2013 14:49:17 -0700 Subject: Don't add cylinders and weightsystems past the MAX We actually should disable the 'Add' button, I guess. Signed-off-by: Dirk Hohndel --- qt-ui/maintab.cpp | 6 ++++++ qt-ui/models.cpp | 2 ++ 2 files changed, 8 insertions(+) (limited to 'qt-ui/models.cpp') diff --git a/qt-ui/maintab.cpp b/qt-ui/maintab.cpp index f8c69ed9c..4174ce5dd 100644 --- a/qt-ui/maintab.cpp +++ b/qt-ui/maintab.cpp @@ -61,6 +61,9 @@ void MainTab::clearStats() void MainTab::on_addCylinder_clicked() { + if (cylindersModel->rowCount() >= MAX_CYLINDERS) + return; + AddCylinderDialog dialog(this); cylinder_t *newCylinder = (cylinder_t*) malloc(sizeof(cylinder_t)); newCylinder->type.description = ""; @@ -85,6 +88,9 @@ void MainTab::on_delCylinder_clicked() void MainTab::on_addWeight_clicked() { + if (weightModel->rowCount() >= MAX_WEIGHTSYSTEMS) + return; + /* this needs a dialog - right now we just fill in a dummy */ weightsystem_t *newWeightsystem = (weightsystem_t *) malloc(sizeof(weightsystem_t)); newWeightsystem->description = "Just testing"; diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 6756002e2..f1bb8d137 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -98,6 +98,7 @@ void CylindersModel::add(cylinder_t* cyl) { if (usedRows[current_dive] >= MAX_CYLINDERS) { free(cyl); + return; } int row = usedRows[current_dive]; @@ -203,6 +204,7 @@ void WeightModel::add(weightsystem_t* weight) { if (usedRows[current_dive] >= MAX_WEIGHTSYSTEMS) { free(weight); + return; } int row = usedRows[current_dive]; -- cgit v1.2.3-70-g09d2 From 764a863082f9337156fc4bcf5c0ecc6ae3d149d6 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Wed, 1 May 2013 23:51:34 -0300 Subject: Added Support for the Trips and Dives on the DiveList model. Now the list and dives will work in the same way that the GTK version does. The code got changed heavly because the old one was just looking at the dives and didn't worked like a tree. small adaptations on the list view and model delegates because of the changes done on this model. Signed-off-by: Tomaz Canabrava --- qt-ui/divelistview.cpp | 2 +- qt-ui/modeldelegates.cpp | 12 +- qt-ui/models.cpp | 445 +++++++++++++++++++++++++---------------------- qt-ui/models.h | 41 ++++- 4 files changed, 284 insertions(+), 216 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp index fb19a7060..45b6cf4d7 100644 --- a/qt-ui/divelistview.cpp +++ b/qt-ui/divelistview.cpp @@ -11,5 +11,5 @@ DiveListView::DiveListView(QWidget *parent) : QTreeView(parent) { setUniformRowHeights(true); - setItemDelegateForColumn(DiveTripModel::RATING, new StarWidgetsDelegate()); + setItemDelegateForColumn(TreeItemDT::RATING, new StarWidgetsDelegate()); } diff --git a/qt-ui/modeldelegates.cpp b/qt-ui/modeldelegates.cpp index 1ac2f46c6..00f10092d 100644 --- a/qt-ui/modeldelegates.cpp +++ b/qt-ui/modeldelegates.cpp @@ -14,10 +14,16 @@ void StarWidgetsDelegate::paint(QPainter* painter, const QStyleOptionViewItem& o return; } - int rating = index.model()->data(index, Qt::DisplayRole).toInt(); + QVariant value = index.model()->data(index, Qt::DisplayRole); - if (option.state & QStyle::State_Selected) - painter->fillRect(option.rect, option.palette.highlight()); + if (!value.isValid()){ + return; + } + + int rating = value.toInt(); + + if(option.state & QStyle::State_Selected) + painter->fillRect(option.rect, option.palette.highlight()); painter->save(); painter->setRenderHint(QPainter::Antialiasing, true); diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 23eea3e48..7b9d8dc16 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -5,7 +5,8 @@ * */ #include "models.h" -#include +#include +#include extern struct tank_info tank_info[100]; @@ -289,59 +290,162 @@ void TankInfoModel::update() * QObject. * */ -class DiveItem + +TreeItemDT::~TreeItemDT() { -public: - DiveItem(): dive(NULL), parentItem(NULL) {} + qDeleteAll(childs); +} - DiveItem(struct dive *d, DiveItem *parent = NULL); +int TreeItemDT::row() const +{ + if (parent) + return parent->childs.indexOf(const_cast(this)); - ~DiveItem() { qDeleteAll(childlist); } + return 0; +} - int diveNumber() const { return dive->number; } - const QString diveDateTime() const { return get_dive_date_string(dive->when); } - int diveDuration() const { return dive->duration.seconds; } - int diveDepth() const { return dive->maxdepth.mm; } - int diveSac() const { return dive->sac; } - int diveOtu() const { return dive->otu; } - int diveMaxcns() const { return dive->maxcns; } +QVariant TreeItemDT::data(int column, int role) const +{ + QVariant ret; + switch (column) { + case NR: + ret = tr("#"); + break; + case DATE: + ret = tr("Date"); + break; + case RATING: + ret = UTF8_BLACKSTAR; + break; + case DEPTH: + ret = (get_units()->length == units::METERS) ? tr("m") : tr("ft"); + break; + case DURATION: + ret = tr("min"); + break; + case TEMPERATURE: + ret = QString("%1%2").arg(UTF8_DEGREE).arg( (get_units()->temperature == units::CELSIUS) ? "C" : "F"); + break; + case TOTALWEIGHT: + ret = (get_units()->weight == units::KG) ? tr("kg") : tr("lbs"); + break; + case SUIT: + ret = tr("Suit"); + break; + case CYLINDER: + ret = tr("Cyl"); + break; + case NITROX: + ret = QString("O%1%").arg(UTF8_SUBSCRIPT_2); + break; + case SAC: + ret = tr("SAC"); + break; + case OTU: + ret = tr("OTU"); + break; + case MAXCNS: + ret = tr("maxCNS"); + break; + case LOCATION: + ret = tr("Location"); + break; + } + return ret; +} + +struct TripItem : public TreeItemDT { + virtual QVariant data(int column, int role) const; + dive_trip_t* trip; +}; + +QVariant TripItem::data(int column, int role) const +{ + QVariant ret; - int diveWeight() const - { - weight_t tw = { total_weight(dive) }; - return tw.grams; + if (column != LOCATION) { + return ret; } - int diveRating() const { return dive->rating; } + switch (role) { + case Qt::DisplayRole: + ret = QString(trip->location); + } + + return ret; +} + +struct DiveItem : public TreeItemDT { + virtual QVariant data(int column, int role) const; + struct dive* dive; QString displayDuration() const; QString displayDepth() const; QString displayTemperature() const; QString displayWeight() const; QString displaySac() const; - const QString diveLocation() const { return dive->location; } - const QString diveSuit() const { return dive->suit; } - DiveItem *parent() const { return parentItem; } - const QList& children() const { return childlist; } - - void addChild(DiveItem* item) { - item->parentItem = this; - childlist.push_back(item); - } /* parent = self */ - -private: - struct dive *dive; - DiveItem *parentItem; - QList childlist; -}; + int weight() const; +}; -DiveItem::DiveItem(struct dive *d, DiveItem *p): - dive(d), - parentItem(p) +QVariant DiveItem::data(int column, int role) const { - if (parentItem) - parentItem->addChild(this); + QVariant retVal; + + if (role == Qt::TextAlignmentRole) { + switch (column) { + case DATE: /* fall through */ + case SUIT: /* fall through */ + case LOCATION: + retVal = Qt::AlignLeft; + break; + default: + retVal = Qt::AlignRight; + break; + } + } + + if (role == Qt::DisplayRole) { + switch (column) { + case NR: + retVal = dive->number; + break; + case DATE: + retVal = QString(get_dive_date_string(dive->when)); + break; + case DEPTH: + retVal = displayDepth(); + break; + case DURATION: + retVal = displayDuration(); + break; + case TEMPERATURE: + retVal = displayTemperature(); + break; + case TOTALWEIGHT: + retVal = displayWeight(); + break; + case SUIT: + retVal = QString(dive->suit); + break; + case SAC: + retVal = displaySac(); + break; + case OTU: + retVal = dive->otu; + break; + case MAXCNS: + retVal = dive->maxcns; + break; + case LOCATION: + retVal = QString(dive->location); + break; + case RATING: + retVal = dive->rating; + break; + } + } + return retVal; } QString DiveItem::displayDepth() const @@ -361,7 +465,6 @@ QString DiveItem::displayDepth() const QString DiveItem::displayDuration() const { int hrs, mins, secs; - secs = dive->duration.seconds % 60; mins = dive->duration.seconds / 60; hrs = mins / 60; @@ -405,215 +508,147 @@ QString DiveItem::displayWeight() const QString str; if (get_units()->weight == units::KG) { - int gr = diveWeight() % 1000; - int kg = diveWeight() / 1000; + int gr = weight() % 1000; + int kg = weight() / 1000; str = QString("%1.%2").arg(kg).arg((unsigned)(gr + 500) / 100); } else { - str = QString("%1").arg((unsigned)(grams_to_lbs(diveWeight()) + 0.5)); + str = QString("%1").arg((unsigned)(grams_to_lbs(weight()) + 0.5)); } + return str; } -DiveTripModel::DiveTripModel(QObject *parent) : QAbstractItemModel(parent) +int DiveItem::weight() const { - rootItem = new DiveItem; - int i; - struct dive *d; - - for_each_dive(i, d) { - new DiveItem(d, rootItem); - } + weight_t tw = { total_weight(dive) }; + return tw.grams; } -Qt::ItemFlags DiveTripModel::flags(const QModelIndex &index) const +DiveTripModel::DiveTripModel(QObject* parent) + : QAbstractItemModel(parent) { - Qt::ItemFlags diveFlags = QAbstractItemModel::flags(index); - if (index.isValid()) { - diveFlags |= Qt::ItemIsSelectable|Qt::ItemIsEnabled; - } - return diveFlags; + rootItem = new TreeItemDT(); + setupModelData(); } +DiveTripModel::~DiveTripModel() +{ + delete rootItem; +} -QVariant DiveTripModel::data(const QModelIndex &index, int role) const +int DiveTripModel::columnCount(const QModelIndex& parent) const +{ + if (parent.isValid()) + return static_cast(parent.internalPointer())->columnCount(); + else + return rootItem->columnCount(); +} + +QVariant DiveTripModel::data(const QModelIndex& index, int role) const { if (!index.isValid()) return QVariant(); - DiveItem *item = static_cast(index.internalPointer()); - - QVariant retVal; - if (role == Qt::TextAlignmentRole) { - switch (index.column()) { - case DATE: /* fall through */ - case SUIT: /* fall through */ - case LOCATION: - retVal = Qt::AlignLeft; - break; - default: - retVal = Qt::AlignRight; - break; - } - } - if (role == Qt::DisplayRole) { - switch (index.column()) { - case NR: - retVal = item->diveNumber(); - break; - case DATE: - retVal = item->diveDateTime(); - break; - case DEPTH: - retVal = item->displayDepth(); - break; - case DURATION: - retVal = item->displayDuration(); - break; - case TEMPERATURE: - retVal = item->displayTemperature(); - break; - case TOTALWEIGHT: - retVal = item->displayWeight(); - break; - case SUIT: - retVal = item->diveSuit(); - break; - case SAC: - retVal = item->displaySac(); - break; - case OTU: - retVal = item->diveOtu(); - break; - case MAXCNS: - retVal = item->diveMaxcns(); - break; - case LOCATION: - retVal = item->diveLocation(); - break; - case RATING: - retVal = item->diveRating(); - break; - } - } - return retVal; -} - + if (role != Qt::DisplayRole) + return QVariant(); -QVariant DiveTripModel::headerData(int section, Qt::Orientation orientation, int role) const -{ - QVariant ret; - if (orientation != Qt::Horizontal) - return ret; + TreeItemDT* item = static_cast(index.internalPointer()); - if (role == Qt::DisplayRole) { - switch(section) { - case NR: - ret = tr("#"); - break; - case DATE: - ret = tr("Date"); - break; - case RATING: - ret = UTF8_BLACKSTAR; - break; - case DEPTH: - if (get_units()->length == units::METERS) - ret = tr("m"); - else - ret = tr("ft"); - break; - case DURATION: - ret = tr("min"); - break; - case TEMPERATURE: - if (get_units()->temperature == units::CELSIUS) - ret = QString("%1%2").arg(UTF8_DEGREE).arg("C"); - else - ret = QString("%1%2").arg(UTF8_DEGREE).arg("F"); - break; - case TOTALWEIGHT: - if (get_units()->weight == units::KG) - ret = tr("kg"); - else - ret = tr("lbs"); - break; - case SUIT: - ret = tr("Suit"); - break; - case CYLINDER: - ret = tr("Cyl"); - break; - case NITROX: - ret = QString("O%1%").arg(UTF8_SUBSCRIPT_2); - break; - case SAC: - ret = tr("SAC"); - break; - case OTU: - ret = tr("OTU"); - break; - case MAXCNS: - ret = tr("maxCNS"); - break; - case LOCATION: - ret = tr("Location"); - break; - } - } - return ret; + return item->data(index.column(), role); } -int DiveTripModel::rowCount(const QModelIndex &parent) const +Qt::ItemFlags DiveTripModel::flags(const QModelIndex& index) const { - /* only allow kids in column 0 */ - if (parent.isValid() && parent.column() > 0) + if (!index.isValid()) return 0; - DiveItem *item = itemForIndex(parent); - return item ? item->children().count() : 0; + + return Qt::ItemIsEnabled | Qt::ItemIsSelectable; } -int DiveTripModel::columnCount(const QModelIndex &parent) const +QVariant DiveTripModel::headerData(int section, Qt::Orientation orientation, + int role) const { - return parent.isValid() && parent.column() != 0 ? 0 : COLUMNS; -} + if (orientation == Qt::Horizontal && role == Qt::DisplayRole) + return rootItem->data(section, role); + return QVariant(); +} -QModelIndex DiveTripModel::index(int row, int column, const QModelIndex &parent) const +QModelIndex DiveTripModel::index(int row, int column, const QModelIndex& parent) +const { - - if (!rootItem || row < 0 || column < 0 || column >= COLUMNS || - (parent.isValid() && parent.column() != 0)) + if (!hasIndex(row, column, parent)) return QModelIndex(); - DiveItem *parentItem = itemForIndex(parent); - Q_ASSERT(parentItem); - if (DiveItem *item = parentItem->children().at(row)) - return createIndex(row, column, item); - return QModelIndex(); -} + TreeItemDT* parentItem = (!parent.isValid()) ? rootItem + : static_cast(parent.internalPointer()); + + TreeItemDT* childItem = parentItem->childs[row]; + return (childItem) ? createIndex(row, column, childItem) + : QModelIndex(); +} -QModelIndex DiveTripModel::parent(const QModelIndex &childIndex) const +QModelIndex DiveTripModel::parent(const QModelIndex& index) const { - if (!childIndex.isValid()) + if (!index.isValid()) return QModelIndex(); - DiveItem *child = static_cast(childIndex.internalPointer()); - DiveItem *parent = child->parent(); + TreeItemDT* childItem = static_cast(index.internalPointer()); + TreeItemDT* parentItem = childItem->parent; - if (parent == rootItem) + if (parentItem == rootItem) return QModelIndex(); - return createIndex(parent->children().indexOf(child), 0, parent); + return createIndex(parentItem->row(), 0, parentItem); } +int DiveTripModel::rowCount(const QModelIndex& parent) const +{ + TreeItemDT* parentItem; + + if (parent.column() > 0) { + return 0; + } + + if (!parent.isValid()) + parentItem = rootItem; + + else + parentItem = static_cast(parent.internalPointer()); + + return parentItem->childs.count(); +} -DiveItem* DiveTripModel::itemForIndex(const QModelIndex &index) const +void DiveTripModel::setupModelData() { - if (index.isValid()) { - DiveItem *item = static_cast(index.internalPointer()); - return item; + int i = dive_table.nr; + + while (--i >= 0) { + struct dive* dive = get_dive(i); + dive_trip_t* trip = dive->divetrip; + + DiveItem* diveItem = new DiveItem(); + diveItem->dive = dive; + + if (!trip) { + diveItem->parent = rootItem; + rootItem->childs.push_back(diveItem); + continue; + } + + if (!trips.keys().contains(trip)) { + TripItem* tripItem = new TripItem(); + tripItem->trip = trip; + tripItem->parent = rootItem; + tripItem->childs.push_back(diveItem); + trips[trip] = tripItem; + rootItem->childs.push_back(tripItem); + continue; + } + + TripItem* tripItem = trips[trip]; + tripItem->childs.push_back(diveItem); } - return rootItem; } diff --git a/qt-ui/models.h b/qt-ui/models.h index 9e4666dc7..7bc7578b8 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -8,8 +8,11 @@ #define MODELS_H #include +#include + #include "../dive.h" #include "../divelist.h" + /* Encapsulates the tank_info global variable * to show on Qt`s Model View System.*/ class TankInfoModel : public QAbstractTableModel { @@ -74,25 +77,49 @@ private: /*! An AbstractItemModel for recording dive trip information such as a list of dives. * */ -class DiveItem; -class DiveTripModel : public QAbstractItemModel -{ + + + +struct TreeItemDT { + Q_DECLARE_TR_FUNCTIONS ( TreeItemDT ); public: enum Column {NR, DATE, RATING, DEPTH, DURATION, TEMPERATURE, TOTALWEIGHT, SUIT, CYLINDER, NITROX, SAC, OTU, MAXCNS, LOCATION, COLUMNS }; + virtual ~TreeItemDT(); + int columnCount() const { + return COLUMNS; + }; + + virtual QVariant data ( int column, int role ) const; + int row() const; + QList childs; + TreeItemDT *parent; +}; + +struct TripItem; + +class DiveTripModel : public QAbstractItemModel +{ + Q_OBJECT + +public: DiveTripModel(QObject *parent = 0); + ~DiveTripModel(); /*reimp*/ Qt::ItemFlags flags(const QModelIndex &index) const; /*reimp*/ QVariant data(const QModelIndex &index, int role) const; - /*reimp*/ QVariant headerData(int section, Qt::Orientation orientation, int role) const; - /*reimp*/ int rowCount(const QModelIndex &parent) const; + /*reimp*/ QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + /*reimp*/ int rowCount(const QModelIndex &parent = QModelIndex()) const; /*reimp*/ int columnCount(const QModelIndex &parent = QModelIndex()) const; /*reimp*/ QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; /*reimp*/ QModelIndex parent(const QModelIndex &child) const; + private: - DiveItem *itemForIndex(const QModelIndex& index) const; - DiveItem *rootItem; + void setupModelData(); + + TreeItemDT *rootItem; + QMap trips; }; #endif -- cgit v1.2.3-70-g09d2 From 6b7797140bcf706b1a0a2eeb70438d8572372b9f Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Wed, 1 May 2013 22:00:08 -0700 Subject: Minor style updates Signed-off-by: Dirk Hohndel --- qt-ui/modeldelegates.cpp | 6 ++---- qt-ui/models.cpp | 53 +++++++++++++++++++++--------------------------- qt-ui/models.h | 4 ---- 3 files changed, 25 insertions(+), 38 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/qt-ui/modeldelegates.cpp b/qt-ui/modeldelegates.cpp index 00f10092d..87629bd1b 100644 --- a/qt-ui/modeldelegates.cpp +++ b/qt-ui/modeldelegates.cpp @@ -10,15 +10,13 @@ void StarWidgetsDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { - if (!index.isValid()){ + if (!index.isValid()) return; - } QVariant value = index.model()->data(index, Qt::DisplayRole); - if (!value.isValid()){ + if (!value.isValid()) return; - } int rating = value.toInt(); diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 6d9bbad39..dd2cf2f84 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -17,9 +17,8 @@ CylindersModel::CylindersModel(QObject* parent): QAbstractTableModel(parent) QVariant CylindersModel::headerData(int section, Qt::Orientation orientation, int role) const { QVariant ret; - if (orientation == Qt::Vertical) { + if (orientation == Qt::Vertical) return ret; - } if (role == Qt::DisplayRole) { switch(section) { @@ -57,9 +56,9 @@ int CylindersModel::columnCount(const QModelIndex& parent) const QVariant CylindersModel::data(const QModelIndex& index, int role) const { QVariant ret; - if (!index.isValid() || index.row() >= MAX_CYLINDERS) { + if (!index.isValid() || index.row() >= MAX_CYLINDERS) return ret; - } + cylinder_t& cyl = current_dive->cylinder[index.row()]; if (role == Qt::DisplayRole) { @@ -152,9 +151,9 @@ int WeightModel::columnCount(const QModelIndex& parent) const QVariant WeightModel::data(const QModelIndex& index, int role) const { QVariant ret; - if (!index.isValid() || index.row() >= MAX_WEIGHTSYSTEMS) { + if (!index.isValid() || index.row() >= MAX_WEIGHTSYSTEMS) return ret; - } + weightsystem_t *ws = ¤t_dive->weightsystem[index.row()]; if (role == Qt::DisplayRole) { @@ -184,9 +183,8 @@ int WeightModel::rowCount(const QModelIndex& parent) const QVariant WeightModel::headerData(int section, Qt::Orientation orientation, int role) const { QVariant ret; - if (orientation == Qt::Vertical) { + if (orientation == Qt::Vertical) return ret; - } if (role == Qt::DisplayRole) { switch(section) { @@ -257,9 +255,11 @@ QVariant TankInfoModel::data(const QModelIndex& index, int role) const } if (role == Qt::DisplayRole) { switch(index.column()) { - case BAR: ret = bar; + case BAR: + ret = bar; break; - case ML: ret = ml; + case ML: + ret = ml; break; case DESCRIPTION: ret = QString(info->name); @@ -300,7 +300,7 @@ int TankInfoModel::rowCount(const QModelIndex& parent) const TankInfoModel::TankInfoModel() : QAbstractTableModel(), rows(-1) { struct tank_info *info = tank_info; - for (info = tank_info ; info->name; info++, rows++); + for (info = tank_info; info->name; info++, rows++); if (rows > -1) { beginInsertRows(QModelIndex(), 0, rows); @@ -315,7 +315,7 @@ void TankInfoModel::update() endRemoveRows(); } struct tank_info *info = tank_info; - for (info = tank_info ; info->name; info++, rows++); + for (info = tank_info; info->name; info++, rows++); if (rows > -1) { beginInsertRows(QModelIndex(), 0, rows); @@ -426,7 +426,6 @@ struct DiveItem : public TreeItemDT { QString displayWeight() const; QString displaySac() const; int weight() const; - }; QVariant DiveItem::data(int column, int role) const @@ -524,11 +523,11 @@ QString DiveItem::displayTemperature() const { QString str; - if (get_units()->temperature == units::CELSIUS) { + if (get_units()->temperature == units::CELSIUS) str = QString::number(mkelvin_to_C(dive->watertemp.mkelvin), 'f', 1); - } else { + else str = QString::number(mkelvin_to_F(dive->watertemp.mkelvin), 'f', 1); - } + return str; } @@ -536,11 +535,11 @@ QString DiveItem::displaySac() const { QString str; - if (get_units()->volume == units::LITER) { + if (get_units()->volume == units::LITER) str = QString::number(dive->sac / 1000, 'f', 1); - } else { + else str = QString::number(ml_to_cuft(dive->sac), 'f', 2); - } + return str; } @@ -566,8 +565,8 @@ int DiveItem::weight() const } -DiveTripModel::DiveTripModel(QObject* parent) - : QAbstractItemModel(parent) +DiveTripModel::DiveTripModel(QObject* parent) : + QAbstractItemModel(parent) { rootItem = new TreeItemDT(); setupModelData(); @@ -622,13 +621,11 @@ const if (!hasIndex(row, column, parent)) return QModelIndex(); - TreeItemDT* parentItem = (!parent.isValid()) ? rootItem - : static_cast(parent.internalPointer()); + TreeItemDT* parentItem = (!parent.isValid()) ? rootItem : static_cast(parent.internalPointer()); TreeItemDT* childItem = parentItem->childs[row]; - return (childItem) ? createIndex(row, column, childItem) - : QModelIndex(); + return (childItem) ? createIndex(row, column, childItem) : QModelIndex(); } QModelIndex DiveTripModel::parent(const QModelIndex& index) const @@ -649,13 +646,11 @@ int DiveTripModel::rowCount(const QModelIndex& parent) const { TreeItemDT* parentItem; - if (parent.column() > 0) { + if (parent.column() > 0) return 0; - } if (!parent.isValid()) parentItem = rootItem; - else parentItem = static_cast(parent.internalPointer()); @@ -678,7 +673,6 @@ void DiveTripModel::setupModelData() rootItem->childs.push_back(diveItem); continue; } - if (!trips.keys().contains(trip)) { TripItem* tripItem = new TripItem(); tripItem->trip = trip; @@ -688,7 +682,6 @@ void DiveTripModel::setupModelData() rootItem->childs.push_back(tripItem); continue; } - TripItem* tripItem = trips[trip]; tripItem->childs.push_back(diveItem); } diff --git a/qt-ui/models.h b/qt-ui/models.h index 52a3498df..d063874d3 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -79,8 +79,6 @@ private: * */ - - struct TreeItemDT { Q_DECLARE_TR_FUNCTIONS ( TreeItemDT ); public: @@ -96,7 +94,6 @@ public: TreeItemDT *parent; }; - struct TripItem; class DiveTripModel : public QAbstractItemModel @@ -115,7 +112,6 @@ public: /*reimp*/ QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; /*reimp*/ QModelIndex parent(const QModelIndex &child) const; - private: void setupModelData(); -- cgit v1.2.3-70-g09d2 From e6ec626c9758d30c3f714ddbf6f79195e710e037 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Thu, 2 May 2013 09:33:51 -0700 Subject: Show trip date (with number of dives) in dive/trip list That was easy :-) Signed-off-by: Dirk Hohndel --- qt-ui/models.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index dd2cf2f84..12a0398a0 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -404,13 +404,15 @@ QVariant TripItem::data(int column, int role) const { QVariant ret; - if (column != LOCATION) { - return ret; - } - - switch (role) { - case Qt::DisplayRole: - ret = QString(trip->location); + if (role == Qt::DisplayRole) { + switch (column) { + case LOCATION: + ret = QString(trip->location); + break; + case DATE: + ret = QString(get_trip_date_string(trip->when, trip->nrdives)); + break; + } } return ret; -- cgit v1.2.3-70-g09d2 From f9c97ff97d072d6a4cb934a50427dc69382281e0 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Thu, 2 May 2013 10:48:09 -0700 Subject: Show cylinder and gas used in the Qt dive list Signed-off-by: Dirk Hohndel --- qt-ui/models.cpp | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'qt-ui/models.cpp') diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 12a0398a0..68f575ec8 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -470,6 +470,12 @@ QVariant DiveItem::data(int column, int role) const case SUIT: retVal = QString(dive->suit); break; + case CYLINDER: + retVal = QString(dive->cylinder[0].type.description); + break; + case NITROX: + retVal = QString(get_nitrox_string(dive)); + break; case SAC: retVal = displaySac(); break; -- cgit v1.2.3-70-g09d2 From c4e2c322a3f6505880b9330e577129e0110ed2ba Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Thu, 2 May 2013 14:05:53 -0700 Subject: Set better column widths in the dive list This code seems rather crude to me. I'm sure this could be done better. This also makes the column alignment work again. Signed-off-by: Dirk Hohndel --- qt-ui/mainwindow.cpp | 34 ++++++++++++++++++++++++++++++++++ qt-ui/models.cpp | 11 +++++------ 2 files changed, 39 insertions(+), 6 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp index 9901e4186..8bf03531f 100644 --- a/qt-ui/mainwindow.cpp +++ b/qt-ui/mainwindow.cpp @@ -14,6 +14,8 @@ #include #include #include +#include +#include #include "divelistview.h" #include "starwidget.h" @@ -32,6 +34,38 @@ MainWindow::MainWindow() : ui(new Ui::MainWindow()), sortModel->setSourceModel(model); ui->ListWidget->setModel(sortModel); + /* figure out appropriate widths for the columns. The strings chosen + * are somewhat random (but at least we're trying to allow them to be + * localized so they are somewhat universal) */ + QFontMetrics fm(QApplication::font()); + int pixelsWide = fm.width(tr("Trip Wed, Mar 29, 2000 (100 dives)")); + ui->ListWidget->setColumnWidth(TreeItemDT::DATE, pixelsWide); + + /* all the columns that have usually up to four numbers plus maybe + * a decimal separator */ + pixelsWide = fm.width("000.0"); + ui->ListWidget->setColumnWidth(TreeItemDT::DEPTH, pixelsWide); + ui->ListWidget->setColumnWidth(TreeItemDT::TEMPERATURE, pixelsWide); + ui->ListWidget->setColumnWidth(TreeItemDT::TOTALWEIGHT, pixelsWide); + ui->ListWidget->setColumnWidth(TreeItemDT::SAC, pixelsWide); + ui->ListWidget->setColumnWidth(TreeItemDT::OTU, pixelsWide); + + /* this one is likely dominated by the header (need extra pixels) */ + pixelsWide = fm.width(tr("maxCNS")) + 10; + ui->ListWidget->setColumnWidth(TreeItemDT::MAXCNS, pixelsWide); + + /* the rest we try to cover with reasonable sample text again */ + pixelsWide = fm.width(" 123456"); + ui->ListWidget->setColumnWidth(TreeItemDT::NR, pixelsWide); + pixelsWide = fm.width("00:00:00"); + ui->ListWidget->setColumnWidth(TreeItemDT::DURATION, pixelsWide); + pixelsWide = fm.width(tr("twin HP119")); + ui->ListWidget->setColumnWidth(TreeItemDT::CYLINDER, pixelsWide); + pixelsWide = fm.width("888888"); + ui->ListWidget->setColumnWidth(TreeItemDT::NITROX, pixelsWide); + pixelsWide = fm.width(tr("7mm wet, farmer johns and jacket")); + ui->ListWidget->setColumnWidth(TreeItemDT::SUIT, pixelsWide); + setWindowIcon(QIcon(":subsurface-icon")); readSettings(); } diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 68f575ec8..5c7fc7a8b 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -434,7 +434,8 @@ QVariant DiveItem::data(int column, int role) const { QVariant retVal; - if (role == Qt::TextAlignmentRole) { + switch (role) { + case Qt::TextAlignmentRole: switch (column) { case DATE: /* fall through */ case SUIT: /* fall through */ @@ -445,9 +446,8 @@ QVariant DiveItem::data(int column, int role) const retVal = Qt::AlignRight; break; } - } - - if (role == Qt::DisplayRole) { + break; + case Qt::DisplayRole: switch (column) { case NR: retVal = dive->number; @@ -492,6 +492,7 @@ QVariant DiveItem::data(int column, int role) const retVal = dive->rating; break; } + break; } return retVal; } @@ -598,8 +599,6 @@ QVariant DiveTripModel::data(const QModelIndex& index, int role) const if (!index.isValid()) return QVariant(); - if (role != Qt::DisplayRole) - return QVariant(); TreeItemDT* item = static_cast(index.internalPointer()); -- cgit v1.2.3-70-g09d2 From 021ef8ad09295a0ad4b5a3450a651637eea8672d Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Thu, 2 May 2013 14:19:15 -0700 Subject: Alternate background colors for dive list This was written with massive hand-holding by Tomaz - actually, this was mostly proposed via IRC by Tomaz and then implemented by me... Right now because of the list-of-lists nature of the model we have the small issue that every trip starts with a dark background dive, even if the trip itself has a dark background. Signed-off-by: Dirk Hohndel --- qt-ui/modeldelegates.cpp | 8 +++++--- qt-ui/models.cpp | 4 ++++ 2 files changed, 9 insertions(+), 3 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/qt-ui/modeldelegates.cpp b/qt-ui/modeldelegates.cpp index 87629bd1b..2adaaebc7 100644 --- a/qt-ui/modeldelegates.cpp +++ b/qt-ui/modeldelegates.cpp @@ -15,14 +15,16 @@ void StarWidgetsDelegate::paint(QPainter* painter, const QStyleOptionViewItem& o QVariant value = index.model()->data(index, Qt::DisplayRole); + if(option.state & QStyle::State_Selected) + painter->fillRect(option.rect, option.palette.highlight()); + else + painter->fillRect(option.rect, index.model()->data(index, Qt::BackgroundRole).value()); + if (!value.isValid()) return; int rating = value.toInt(); - if(option.state & QStyle::State_Selected) - painter->fillRect(option.rect, option.palette.highlight()); - painter->save(); painter->setRenderHint(QPainter::Antialiasing, true); diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 5c7fc7a8b..373770404 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -7,6 +7,8 @@ #include "models.h" #include #include +#include +#include extern struct tank_info tank_info[100]; @@ -599,6 +601,8 @@ QVariant DiveTripModel::data(const QModelIndex& index, int role) const if (!index.isValid()) return QVariant(); + if (role == Qt::BackgroundRole) + return QBrush(QColor(index.row() % 2 ? Qt::white : QColor(Qt::lightGray).lighter(120))); TreeItemDT* item = static_cast(index.internalPointer()); -- cgit v1.2.3-70-g09d2 From 82b1b04920fdd5882e0cbf28c9871c2ddd404af8 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Thu, 2 May 2013 19:27:36 -0300 Subject: Test the CSS for styling the TableView This is a test and I shouldn't be taken seriously. Signed-off-by: Tomaz Canabrava Signed-off-by: Dirk Hohndel --- qt-ui/divelistview.cpp | 2 ++ qt-ui/mainwindow.ui | 79 +++++++++++++++++++++++++++++++++++++++++++++++- qt-ui/modeldelegates.cpp | 14 +++++++-- qt-ui/modeldelegates.h | 7 +++-- qt-ui/models.cpp | 5 ++- 5 files changed, 99 insertions(+), 8 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp index 45b6cf4d7..0bf0b35ba 100644 --- a/qt-ui/divelistview.cpp +++ b/qt-ui/divelistview.cpp @@ -7,6 +7,8 @@ #include "divelistview.h" #include "models.h" #include "modeldelegates.h" +#include +#include DiveListView::DiveListView(QWidget *parent) : QTreeView(parent) { diff --git a/qt-ui/mainwindow.ui b/qt-ui/mainwindow.ui index 073476918..1d31f1cd3 100644 --- a/qt-ui/mainwindow.ui +++ b/qt-ui/mainwindow.ui @@ -28,9 +28,86 @@ + + QTreeView { + show-decoration-selected: 1; + } + + QTreeView::item { + border: 1px solid #d9d9d9; + border-top-color: transparent; + border-bottom-color: transparent; + padding: 2px; + } + + QTreeView::item:hover { + background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e7effd, stop: 1 #cbdaf1); + border: 1px solid #bfcde4; + } + + QTreeView::item:selected { + border: 1px solid #567dbc; + } + + QTreeView::item:selected:active{ + background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6ea1f1, stop: 1 #567dbc); + } + + QTreeView::item:selected:!active { + background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6b9be8, stop: 1 #577fbf); + } + + +/* + QTreeView::branch { + background: palette(base); + } + + QTreeView::branch:has-siblings:!adjoins-item { + background: cyan; + } + + QTreeView::branch:has-siblings:adjoins-item { + background: red; + } + + QTreeView::branch:!has-children:!has-siblings:adjoins-item { + background: blue; + } + + QTreeView::branch:closed:has-children:has-siblings { + background: pink; + } + + QTreeView::branch:has-children:!has-siblings:closed { + background: gray; + } + + QTreeView::branch:open:has-children:has-siblings { + background: magenta; + } + + QTreeView::branch:open:has-children:!has-siblings { + background: green; + } +*/ + + + + true + + + true + true + + true + + + true + @@ -42,7 +119,7 @@ 0 0 763 - 20 + 19 diff --git a/qt-ui/modeldelegates.cpp b/qt-ui/modeldelegates.cpp index 87629bd1b..0b3231583 100644 --- a/qt-ui/modeldelegates.cpp +++ b/qt-ui/modeldelegates.cpp @@ -7,9 +7,21 @@ #include #include #include +#include +#include +#include +#include + +StarWidgetsDelegate::StarWidgetsDelegate(QWidget* parent): + QStyledItemDelegate(parent), + parentWidget(parent) +{ + +} void StarWidgetsDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { + QStyledItemDelegate::paint(painter, option, index); if (!index.isValid()) return; @@ -20,8 +32,6 @@ void StarWidgetsDelegate::paint(QPainter* painter, const QStyleOptionViewItem& o int rating = value.toInt(); - if(option.state & QStyle::State_Selected) - painter->fillRect(option.rect, option.palette.highlight()); painter->save(); painter->setRenderHint(QPainter::Antialiasing, true); diff --git a/qt-ui/modeldelegates.h b/qt-ui/modeldelegates.h index eacbb5a1e..5f90a3061 100644 --- a/qt-ui/modeldelegates.h +++ b/qt-ui/modeldelegates.h @@ -1,12 +1,15 @@ #ifndef MODELDELEGATES_H #define MODELDELEGATES_H -#include +#include -class StarWidgetsDelegate : public QAbstractItemDelegate { +class StarWidgetsDelegate : public QStyledItemDelegate { Q_OBJECT public: + explicit StarWidgetsDelegate(QWidget* parent = 0); virtual void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; virtual QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const; +private: + QWidget *parentWidget; }; #endif diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 68f575ec8..061ffe418 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -7,6 +7,8 @@ #include "models.h" #include #include +#include +#include extern struct tank_info tank_info[100]; @@ -598,9 +600,6 @@ QVariant DiveTripModel::data(const QModelIndex& index, int role) const if (!index.isValid()) return QVariant(); - if (role != Qt::DisplayRole) - return QVariant(); - TreeItemDT* item = static_cast(index.internalPointer()); return item->data(index.column(), role); -- cgit v1.2.3-70-g09d2 From 696c9ccacd24392ea63477c5ec8a25d6649aedf7 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Thu, 2 May 2013 20:32:57 -0300 Subject: Added code to Select a dive, fixed minor annoyances. Signed-off-by: Tomaz Canabrava Signed-off-by: Dirk Hohndel --- dive.c | 9 +++++++++ dive.h | 2 ++ qt-ui/mainwindow.cpp | 12 ++++++++++++ qt-ui/mainwindow.h | 3 +++ qt-ui/mainwindow.ui | 37 +++---------------------------------- qt-ui/modeldelegates.cpp | 3 ++- qt-ui/models.cpp | 12 +++++++++--- qt-ui/models.h | 6 +++++- 8 files changed, 45 insertions(+), 39 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/dive.c b/dive.c index 34025d68c..2c2307e41 100644 --- a/dive.c +++ b/dive.c @@ -1810,6 +1810,15 @@ struct dive *merge_dives(struct dive *a, struct dive *b, int offset, gboolean pr return res; } +int get_index_for_dive(struct dive *dive) { + int i; + struct dive *d; + for_each_dive(i, d) + if (d == dive) + return i; + return -1; +} + struct dive *find_dive_including(timestamp_t when) { int i; diff --git a/dive.h b/dive.h index f2153587f..c13cac0aa 100644 --- a/dive.h +++ b/dive.h @@ -350,6 +350,8 @@ struct dive { struct divecomputer dc; }; +extern int get_index_for_dive(struct dive *dive); + static inline int dive_has_gps_location(struct dive *dive) { return dive->latitude.udeg || dive->longitude.udeg; diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp index 9901e4186..ae2794e3c 100644 --- a/qt-ui/mainwindow.cpp +++ b/qt-ui/mainwindow.cpp @@ -23,6 +23,7 @@ #include "../divelist.h" #include "../pref.h" #include "modeldelegates.h" +#include "models.h" MainWindow::MainWindow() : ui(new Ui::MainWindow()), model(new DiveTripModel(this)), @@ -31,11 +32,22 @@ MainWindow::MainWindow() : ui(new Ui::MainWindow()), ui->setupUi(this); sortModel->setSourceModel(model); ui->ListWidget->setModel(sortModel); + connect(ui->ListWidget, SIGNAL(activated(QModelIndex)), this, SLOT(diveSelected(QModelIndex))); setWindowIcon(QIcon(":subsurface-icon")); readSettings(); } +void MainWindow::diveSelected(const QModelIndex& index) +{ + struct dive *dive = (struct dive*) index.model()->data(index, TreeItemDT::DIVE_ROLE).value(); + + if (dive) + selected_dive = get_index_for_dive(dive); + + // Here should be the code to update the other widgets. +} + void MainWindow::on_actionNew_triggered() { qDebug("actionNew"); diff --git a/qt-ui/mainwindow.h b/qt-ui/mainwindow.h index eece91ade..e94cb5b7c 100644 --- a/qt-ui/mainwindow.h +++ b/qt-ui/mainwindow.h @@ -8,6 +8,7 @@ #define MAINWINDOW_H #include +#include class QSortFilterProxyModel; class DiveTripModel; @@ -66,6 +67,8 @@ private Q_SLOTS: void on_actionAboutSubsurface_triggered(); void on_actionUserManual_triggered(); + void diveSelected(const QModelIndex& index); + protected: void closeEvent(QCloseEvent *); diff --git a/qt-ui/mainwindow.ui b/qt-ui/mainwindow.ui index 1d31f1cd3..fe97d98b4 100644 --- a/qt-ui/mainwindow.ui +++ b/qt-ui/mainwindow.ui @@ -57,45 +57,14 @@ background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6b9be8, stop: 1 #577fbf); } - -/* - QTreeView::branch { - background: palette(base); - } - - QTreeView::branch:has-siblings:!adjoins-item { - background: cyan; - } - - QTreeView::branch:has-siblings:adjoins-item { - background: red; - } - - QTreeView::branch:!has-children:!has-siblings:adjoins-item { - background: blue; - } - - QTreeView::branch:closed:has-children:has-siblings { - background: pink; - } - - QTreeView::branch:has-children:!has-siblings:closed { - background: gray; - } - - QTreeView::branch:open:has-children:has-siblings { - background: magenta; - } - - QTreeView::branch:open:has-children:!has-siblings { - background: green; - } -*/ true + + QAbstractItemView::ExtendedSelection + true diff --git a/qt-ui/modeldelegates.cpp b/qt-ui/modeldelegates.cpp index 0b3231583..33c1717e6 100644 --- a/qt-ui/modeldelegates.cpp +++ b/qt-ui/modeldelegates.cpp @@ -22,10 +22,11 @@ StarWidgetsDelegate::StarWidgetsDelegate(QWidget* parent): void StarWidgetsDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { QStyledItemDelegate::paint(painter, option, index); + if (!index.isValid()) return; - QVariant value = index.model()->data(index, Qt::DisplayRole); + QVariant value = index.model()->data(index, TreeItemDT::STAR_ROLE); if (!value.isValid()) return; diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 061ffe418..bdc3a7576 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -490,11 +490,17 @@ QVariant DiveItem::data(int column, int role) const case LOCATION: retVal = QString(dive->location); break; - case RATING: - retVal = dive->rating; - break; } } + + if(role == STAR_ROLE){ + retVal = dive->rating; + } + + if(role == DIVE_ROLE){ + retVal = QVariant::fromValue(dive); + } + return retVal; } diff --git a/qt-ui/models.h b/qt-ui/models.h index d063874d3..307cdf5c3 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -82,7 +82,11 @@ private: struct TreeItemDT { Q_DECLARE_TR_FUNCTIONS ( TreeItemDT ); public: - enum Column {NR, DATE, RATING, DEPTH, DURATION, TEMPERATURE, TOTALWEIGHT, SUIT, CYLINDER, NITROX, SAC, OTU, MAXCNS, LOCATION, COLUMNS }; + enum Column {NR, DATE, RATING, DEPTH, DURATION, TEMPERATURE, TOTALWEIGHT, + SUIT, CYLINDER, NITROX, SAC, OTU, MAXCNS, LOCATION, DIVE, COLUMNS }; + + enum ExtraRoles{STAR_ROLE = Qt::UserRole + 1, DIVE_ROLE}; + virtual ~TreeItemDT(); int columnCount() const { return COLUMNS; -- cgit v1.2.3-70-g09d2 From b75a89aa868d39be29d2bb220a6e0c50d5a2b0ac Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Mon, 6 May 2013 20:36:37 -0700 Subject: Start populating the maintab Dive Info widget Establish some useful helpers and use them when updating the values. One of the helpers (from statistics.c) puzzlingly doesn't link - so that's ifdefed out. Also had to re-arrange the settings reading code (it came too late) and to extract the expanding code of the top dive from the settings reading code (as it had no business being there to begin with). Signed-off-by: Dirk Hohndel --- Makefile | 1 + qt-gui.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ qt-ui/maintab.cpp | 23 +++++++++++++++++++++- qt-ui/mainwindow.cpp | 14 ++++++-------- qt-ui/models.cpp | 1 + statistics.c | 15 +++++++++++++++ statistics.h | 1 + 7 files changed, 100 insertions(+), 9 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/Makefile b/Makefile index 338f81a7c..add90b308 100644 --- a/Makefile +++ b/Makefile @@ -57,6 +57,7 @@ SOURCES = \ profile.c \ save-xml.c \ sha1.c \ + statistics.c \ time.c \ qt-gui.cpp \ qt-ui/addcylinderdialog.cpp \ diff --git a/qt-gui.cpp b/qt-gui.cpp index a4801f760..1e2c86e69 100644 --- a/qt-gui.cpp +++ b/qt-gui.cpp @@ -133,5 +133,59 @@ void set_dc_nickname(struct dive *dive) /* needs Qt implementation */ } +QString get_depth_string(depth_t depth, bool showunit) +{ + if (prefs.units.length == units::METERS) { + double meters = depth.mm / 1000.0; + return QString("%1%2").arg(meters, 0, 'f', meters >= 20.0 ? 0 : 1 ).arg(showunit ? _("m") : ""); + } else { + double feet = mm_to_feet(depth.mm); + return QString("%1%2").arg(feet, 0, 'f', 1). arg(showunit ? _("ft") : ""); + } +} + +QString get_weight_string(weight_t weight, bool showunit) +{ + if (prefs.units.weight == units::KG) { + double kg = weight.grams / 1000.0; + return QString("%1%2").arg(kg, 0, 'f', kg >= 20.0 ? 0 : 1 ).arg(showunit ? _("kg") : ""); + } else { + double lbs = grams_to_lbs(weight.grams); + return QString("%1%2").arg(lbs, 0, 'f', lbs >= 40.0 ? 0 : 1 ).arg(showunit ? _("lbs") : ""); + } +} + +QString get_temperature_string(temperature_t temp, bool showunit) +{ + if (prefs.units.temperature == units::CELSIUS) { + double celsius = mkelvin_to_C(temp.mkelvin); + return QString("%1%2").arg(celsius, 0, 'f', 1).arg(showunit ? _("C") : ""); + } else { + double fahrenheit = mkelvin_to_F(temp.mkelvin); + return QString("%1%2").arg(fahrenheit, 0, 'f', 1).arg(showunit ? _("F") : ""); + } +} + +QString get_volume_string(volume_t volume, bool showunit) +{ + if (prefs.units.volume == units::LITER) { + double liter = volume.mliter / 1000.0; + return QString("%1%2").arg(liter, 0, 'f', liter >= 40.0 ? 0 : 1 ).arg(showunit ? _("l") : ""); + } else { + double cuft = ml_to_cuft(volume.mliter); + return QString("%1%2").arg(cuft, 0, 'f', cuft >= 20.0 ? 0 : (cuft >= 2.0 ? 1 : 2)).arg(showunit ? _("cuft") : ""); + } +} + +QString get_pressure_string(pressure_t pressure, bool showunit) +{ + if (prefs.units.pressure == units::BAR) { + double bar = pressure.mbar / 1000.0; + return QString("%1%2").arg(bar, 0, 'f', 1).arg(showunit ? _("bar") : ""); + } else { + double psi = mbar_to_PSI(pressure.mbar); + return QString("%1%2").arg(psi, 0, 'f', 0).arg(showunit ? _("psi") : ""); + } +} #include "qt-gui.moc" diff --git a/qt-ui/maintab.cpp b/qt-ui/maintab.cpp index df6ee69eb..5f668b2be 100644 --- a/qt-ui/maintab.cpp +++ b/qt-ui/maintab.cpp @@ -8,6 +8,8 @@ #include "ui_maintab.h" #include "addcylinderdialog.h" #include "addweightsystemdialog.h" +#include "../helpers.h" +#include "../statistics.h" #include @@ -66,6 +68,7 @@ void MainTab::clearStats() else \ ui->field->setText(d->field) + void MainTab::updateDiveInfo(int dive) { // So, this is what happens now: @@ -77,7 +80,7 @@ void MainTab::updateDiveInfo(int dive) // open the file maintab.ui on the designer // click on the item and check its objectName, // the access is ui->objectName from here on. - + volume_t sacVal; struct dive *d = get_dive(dive); UPDATE_TEXT(d, notes); UPDATE_TEXT(d, location); @@ -88,6 +91,24 @@ void MainTab::updateDiveInfo(int dive) ui->rating->setCurrentStars(d->rating); else ui->rating->setCurrentStars(0); + ui->maximumDepthText->setText(get_depth_string(d->maxdepth, TRUE)); + ui->averageDepthText->setText(get_depth_string(d->meandepth, TRUE)); + sacVal.mliter = d ? d->sac : 0; + ui->sacText->setText(get_volume_string(sacVal, TRUE).append("/min")); + ui->otuText->setText(QString("%1").arg( d ? d->otu : 0)); + ui->waterTemperatureText->setText(d ? get_temperature_string(d->watertemp, TRUE) : ""); + ui->airTemperatureText->setText(d ? get_temperature_string(d->airtemp, TRUE) : ""); + if (d && d->surface_pressure.mbar) + /* this is ALWAYS displayed in mbar */ + ui->airPressureText->setText(QString("%1mbar").arg(d->surface_pressure.mbar)); + else + ui->airPressureText->setText(QString("")); +#if 0 /* this fails to link, even though the function is defined in statistics.c / statistics.h */ + if (d) + ui->gasUsedText->setText(get_volume_string(get_gas_used(d), TRUE)); + else +#endif + ui->gasUsedText->setText(""); } void MainTab::on_addCylinder_clicked() diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp index e8fe80460..56ae64b0c 100644 --- a/qt-ui/mainwindow.cpp +++ b/qt-ui/mainwindow.cpp @@ -32,14 +32,17 @@ MainWindow::MainWindow() : ui(new Ui::MainWindow()), sortModel(new QSortFilterProxyModel()) { ui->setupUi(this); + readSettings(); sortModel->setSourceModel(model); ui->ListWidget->setModel(sortModel); setWindowIcon(QIcon(":subsurface-icon")); - connect(ui->ListWidget->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(dive_selection_changed(QItemSelection,QItemSelection))); - - readSettings(); + QModelIndex firstDiveOrTrip = sortModel->index(0,0); + if (sortModel->index(0,0, firstDiveOrTrip).isValid()) + ui->ListWidget->setCurrentIndex(sortModel->index(0,0, firstDiveOrTrip)); + else + ui->ListWidget->setCurrentIndex(firstDiveOrTrip); } void MainWindow::on_actionNew_triggered() @@ -343,11 +346,6 @@ void MainWindow::readSettings() } ui->ListWidget->collapseAll(); ui->ListWidget->expand(sortModel->index(0,0)); - QModelIndex firstDiveOrTrip = sortModel->index(0,0); - if (sortModel->index(0,0, firstDiveOrTrip).isValid()) - ui->ListWidget->setCurrentIndex(sortModel->index(0,0, firstDiveOrTrip)); - else - ui->ListWidget->setCurrentIndex(firstDiveOrTrip); settings.endGroup(); settings.beginGroup("Units"); GET_UNIT(v, "feet", length, units::METERS, units::FEET); diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index eb4d8974b..9a6edce98 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -677,6 +677,7 @@ void DiveTripModel::setupModelData() while (--i >= 0) { struct dive* dive = get_dive(i); + update_cylinder_related_info(dive); dive_trip_t* trip = dive->divetrip; DiveItem* diveItem = new DiveItem(); diff --git a/statistics.c b/statistics.c index 7532e346e..bee5837d6 100644 --- a/statistics.c +++ b/statistics.c @@ -267,3 +267,18 @@ void get_selected_dives_text(char *buffer, int size) } } +volume_t get_gas_used(struct dive *dive) +{ + int idx; + volume_t gas_used = { 0 }; + for (idx = 0; idx < MAX_CYLINDERS; idx++) { + cylinder_t *cyl = &dive->cylinder[idx]; + pressure_t start, end; + + start = cyl->start.mbar ? cyl->start : cyl->sample_start; + end = cyl->end.mbar ?cyl->sample_end : cyl->sample_end; + if (start.mbar && end.mbar) + gas_used.mliter += gas_volume(cyl, start) - gas_volume(cyl, end); + } + return gas_used; +} diff --git a/statistics.h b/statistics.h index d2709ee93..95f2957e8 100644 --- a/statistics.h +++ b/statistics.h @@ -31,3 +31,4 @@ extern char *get_time_string(int seconds, int maxdays); extern char *get_minutes(int seconds); extern void process_all_dives(struct dive *dive, struct dive **prev_dive); extern void get_selected_dives_text(char *buffer, int size); +extern volume_t get_gas_used(struct dive *dive); -- cgit v1.2.3-70-g09d2 From 5105d6a33316e87fe59652c461df8ad7ca07fb0e Mon Sep 17 00:00:00 2001 From: Amit Chaudhuri Date: Sat, 11 May 2013 11:38:24 +0100 Subject: Fix crash in DiveTripModel. A left click in the treeview header leads to a call to createIndex which results in a null pointer dereference. Signed-off-by: Amit Chaudhuri Signed-off-by: Dirk Hohndel --- qt-ui/models.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'qt-ui/models.cpp') diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 9a6edce98..90143b912 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -650,7 +650,7 @@ QModelIndex DiveTripModel::parent(const QModelIndex& index) const TreeItemDT* childItem = static_cast(index.internalPointer()); TreeItemDT* parentItem = childItem->parent; - if (parentItem == rootItem) + if (parentItem == rootItem || !parentItem) return QModelIndex(); return createIndex(parentItem->row(), 0, parentItem); -- cgit v1.2.3-70-g09d2 From ee7f579242568d86e7ec744c17585066c30fa943 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Mon, 13 May 2013 22:14:59 -0300 Subject: Trying to make the DiveList selection behave correctly And rip out all the code that Dirk put there to do that. Signed-off-by: Tomaz Canabrava Signed-off-by: Dirk Hohndel --- qt-ui/divelistview.cpp | 59 +++++++++++++++- qt-ui/divelistview.h | 11 +++ qt-ui/mainwindow.cpp | 188 +------------------------------------------------ qt-ui/mainwindow.h | 9 --- qt-ui/models.cpp | 7 +- 5 files changed, 73 insertions(+), 201 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp index 0bf0b35ba..af0ef8b0c 100644 --- a/qt-ui/divelistview.cpp +++ b/qt-ui/divelistview.cpp @@ -9,9 +9,66 @@ #include "modeldelegates.h" #include #include +#include +#include -DiveListView::DiveListView(QWidget *parent) : QTreeView(parent) + +DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelection(false) { setUniformRowHeights(true); setItemDelegateForColumn(TreeItemDT::RATING, new StarWidgetsDelegate()); + +} + +void DiveListView::setModel(QAbstractItemModel* model) +{ + QTreeView::setModel(model); +} + +void DiveListView::setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags command) +{ + if (mouseClickSelection) + QTreeView::setSelection(rect, command); +} + +void DiveListView::mousePressEvent(QMouseEvent* event) +{ + mouseClickSelection = true; + QTreeView::mousePressEvent(event); +} + +void DiveListView::mouseReleaseEvent(QMouseEvent* event) +{ + mouseClickSelection = false; + QTreeView::mouseReleaseEvent(event); +} + +void DiveListView::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected) +{ + Q_FOREACH(const QModelIndex& index, deselected.indexes()) { + const QAbstractItemModel *model = index.model(); + struct dive *dive = (struct dive*) model->data(index, TreeItemDT::DIVE_ROLE).value(); + if (!dive) { // is's a trip! + if (model->rowCount(index)) { + expand(index); // leave this - even if it looks like it shouldn't be here. looks like I'v found a Qt bug. + } + } + } + + Q_FOREACH(const QModelIndex& index, selected.indexes()) { + const QAbstractItemModel *model = index.model(); + struct dive *dive = (struct dive*) model->data(index, TreeItemDT::DIVE_ROLE).value(); + if (!dive) { // is's a trip! + if (model->rowCount(index)) { + expand(index); + QItemSelection selection; + selection.select(index.child(0,0), index.child(model->rowCount(index) -1 , 0)); + selectionModel()->select(selection, QItemSelectionModel::Select | QItemSelectionModel::Rows); + selectionModel()->setCurrentIndex(index, QItemSelectionModel::Select | QItemSelectionModel::NoUpdate); + } + } + else { + expand(index.parent()); + } + } } diff --git a/qt-ui/divelistview.h b/qt-ui/divelistview.h index be9774c5c..9cccd39ae 100644 --- a/qt-ui/divelistview.h +++ b/qt-ui/divelistview.h @@ -20,6 +20,17 @@ class DiveListView : public QTreeView { public: DiveListView(QWidget *parent = 0); + + void selectionChanged(const QItemSelection& selected, const QItemSelection& deselected); + void setModel(QAbstractItemModel* model); + + void mousePressEvent(QMouseEvent* event); + void mouseReleaseEvent(QMouseEvent* event); + + void setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags command); + +private: + bool mouseClickSelection; }; #endif // DIVELISTVIEW_H diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp index 3766e2187..58cb86286 100644 --- a/qt-ui/mainwindow.cpp +++ b/qt-ui/mainwindow.cpp @@ -36,24 +36,12 @@ MainWindow::MainWindow() : ui(new Ui::MainWindow()), sortModel->setSourceModel(model); ui->ListWidget->setModel(sortModel); setWindowIcon(QIcon(":subsurface-icon")); - connect(ui->ListWidget->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), - this, SLOT(dive_selection_changed(QItemSelection,QItemSelection))); + QModelIndex firstDiveOrTrip = sortModel->index(0,0); if (sortModel->index(0,0, firstDiveOrTrip).isValid()) ui->ListWidget->setCurrentIndex(sortModel->index(0,0, firstDiveOrTrip)); else ui->ListWidget->setCurrentIndex(firstDiveOrTrip); - -#if 0 - QAction *actionNextDive = new QAction(this); - addAction(actionNextDive); - actionNextDive->setShortcut(Qt::Key_Down); - connect(actionNextDive, SIGNAL(triggered()), this, SLOT(nextDive_triggered())); - QAction *actionPreviousDive = new QAction(this); - addAction(actionPreviousDive); - actionPreviousDive->setShortcut(Qt::Key_Up); - connect(actionPreviousDive, SIGNAL(triggered()), this, SLOT(previousDive_triggered())); -#endif } void MainWindow::redrawProfile() @@ -61,98 +49,6 @@ void MainWindow::redrawProfile() ui->ProfileWidget->plot(get_dive(selected_dive)); } -#if 0 -void MainWindow::nextDive_triggered() -{ - // Get the current Selection: - QItemSelectionModel *m = ui->ListWidget->selectionModel(); - QModelIndexList selection = m->selectedRows(); - - if (!selection.size()) - return; - - // check if it's a dive or trip: - QModelIndex index = selection.first(); - struct dive *d = (struct dive*) index.data(TreeItemDT::DIVE_ROLE).value(); - const QAbstractItemModel *model = index.model(); - - QItemSelectionModel::SelectionFlags flags = (QItemSelectionModel::Select | QItemSelectionModel::Rows); - - if (d) { - // it's a dive. - QModelIndex trip = index.parent(); - - // checks if it's the last dive on a trip list: - if (index.row() == model->rowCount(trip) - 1) { - // selects a trip. - QModelIndex nexttrip = model->index(trip.row()+1, trip.column(), trip.parent()); - if (nexttrip.isValid()) { - m->clear(); - m->select(nexttrip, flags); - } - } else { - m->clear(); - m->select(model->index(index.row()+1, index.column(), index.parent()), flags); - } - } else { - // it's a trip (and we have no empty trips, so there is a first child) - QModelIndex child = index.child(0,0); - m->select(model->index(0, index.column(), index), flags); - } -} - -void MainWindow::previousDive_triggered() -{ - // Get the current Selection: - QItemSelectionModel *m = ui->ListWidget->selectionModel(); - QModelIndexList selection = m->selectedRows(); - - if (!selection.size()) - return; - - // check if it's a dive or trip: - QModelIndex index = selection.first(); - struct dive *d = (struct dive*) index.data(TreeItemDT::DIVE_ROLE).value(); - const QAbstractItemModel *model = index.model(); - - QItemSelectionModel::SelectionFlags flags = (QItemSelectionModel::Select | QItemSelectionModel::Rows); - - if (d) { - // it's a dive. - QModelIndex trip = index.parent(); - - // checks if it's the first dive on a trip list: - if (index.row() == 0) { - if (trip.isValid()) { - // select the trip this dive is in - m->clear(); - m->select(model->index(trip.row(), trip.column(), trip.parent()),flags); - } - } else { - // select the previous dive - m->clear(); - m->select(model->index(index.row() - 1, index.column(), index.parent()), flags); - } - } else { - // it's a trip. - if (index.row() != 0) { - QModelIndex prevtrip = index.sibling(index.row() - 1, 0); - if (!prevtrip.isValid()) - return; - int cnt = prevtrip.model()->rowCount(); - QModelIndex child = prevtrip.child(prevtrip.model()->rowCount() - 1, 0); - /* I don't understand why this gives me incorrect rowCount... */ - while(!child.isValid() && cnt > 0) - child = prevtrip.child(--cnt, 0); - if (!child.isValid()) - return; - m->clear(); - m->select(model->index(child.row(), index.column(), prevtrip), flags); - } - } -} -#endif - void MainWindow::on_actionNew_triggered() { qDebug("actionNew"); @@ -188,88 +84,6 @@ void MainWindow::on_actionOpen_triggered() ui->ListWidget->sortByColumn(0, Qt::DescendingOrder); } -void MainWindow::dive_selection_changed(const QItemSelection& newSelection, const QItemSelection& oldSelection) -{ - int cnt, i; - /* first deselect the dives that are no longer selected */ - Q_FOREACH(const QModelIndex& deselect, oldSelection.indexes()) { - struct dive *d = (struct dive*) deselect.data(TreeItemDT::DIVE_ROLE).value(); - if (!d) { - // this is a trip - if just the trip is deselected but not its children, - // then we manually need to deselect its children - const QAbstractItemModel *model = deselect.model(); - cnt = model->rowCount(deselect); - if (cnt == 0) - continue; - for (i = 0; i < cnt; i++) { - QModelIndex child = deselect.child(i,0); - if (oldSelection.contains(child)) - break; - } - // if none of the dives were in the deselect list (so the user only ctrl-clicked - // on the trip header) then manually deselect all the dives - if (i == model->rowCount(deselect)) { - QItemSelectionModel::SelectionFlags flags = (QItemSelectionModel::Deselect | - QItemSelectionModel::Rows); - QItemSelection removedDives = QItemSelection(); - removedDives.select(deselect.child(0,0), deselect.child(i - 1,0)); - ui->ListWidget->selectionModel()->select(removedDives,flags); - } - } - if (!d || !d->selected) - continue; - deselect_dive(get_divenr(d)); - } - /* then select the newly selected dives */ - bool needToScroll = TRUE; - Q_FOREACH(const QModelIndex& select, newSelection.indexes()) { - struct dive *d = (struct dive*) select.data(TreeItemDT::DIVE_ROLE).value(); - if (!d) { - // this is a trip - const QAbstractItemModel *model = select.model(); - cnt = model->rowCount(select); - if (cnt == 0) - continue; - for (i = 0; i < cnt; i++) { - QModelIndex child = select.child(i,0); - if (newSelection.contains(child)) - break; - } - // if just the trip header was clicked and none of its children, - // select all of them - if (i == model->rowCount(select)) { - if (needToScroll) { - // make sure the trip header is visible - needToScroll = FALSE; - ui->ListWidget->scrollTo(select, QAbstractItemView::PositionAtCenter); - } - QItemSelectionModel::SelectionFlags flags = (QItemSelectionModel::Select | - QItemSelectionModel::Rows); - QItemSelection addedDives = QItemSelection(); - addedDives.select(select.child(0,0), select.child(i - 1,0)); - ui->ListWidget->selectionModel()->select(addedDives,flags); - } - } - if (!d || d->selected) - continue; - select_dive(get_divenr(d)); - if (needToScroll) { - // make sure at least one of them is visible in the list - // and if this is the first dive of a trip, make the trip visible, too - needToScroll = FALSE; - if (select.row() == 0 && d->divetrip && select.parent().isValid()) - ui->ListWidget->scrollTo(select.parent(), QAbstractItemView::PositionAtCenter); - else - ui->ListWidget->scrollTo(select, QAbstractItemView::PositionAtCenter); - } else { - // but all selected dives should be in expanded trips - ui->ListWidget->expand(select.parent()); - } - } - redrawProfile(); - ui->InfoWidget->updateDiveInfo(selected_dive); -} - void MainWindow::on_actionSave_triggered() { qDebug("actionSave"); diff --git a/qt-ui/mainwindow.h b/qt-ui/mainwindow.h index 124f70f33..084818319 100644 --- a/qt-ui/mainwindow.h +++ b/qt-ui/mainwindow.h @@ -69,15 +69,6 @@ private Q_SLOTS: void on_actionAboutSubsurface_triggered(); void on_actionUserManual_triggered(); -#if 0 - /* keyboard actions */ - void nextDive_triggered(); - void previousDive_triggered(); -#endif - - void dive_selection_changed(const QItemSelection& newSelection, - const QItemSelection& oldSelection); - protected: void closeEvent(QCloseEvent *); diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 90143b912..68ab402cd 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -660,15 +660,14 @@ int DiveTripModel::rowCount(const QModelIndex& parent) const { TreeItemDT* parentItem; - if (parent.column() > 0) - return 0; - if (!parent.isValid()) parentItem = rootItem; else parentItem = static_cast(parent.internalPointer()); - return parentItem->childs.count(); + int amount = parentItem->childs.count(); + + return amount; } void DiveTripModel::setupModelData() -- cgit v1.2.3-70-g09d2 From f9598f062cf8684dd92ac56530c3758e3b1a6d9d Mon Sep 17 00:00:00 2001 From: Henrik Brautaset Aronsen Date: Tue, 14 May 2013 09:28:30 +0200 Subject: Clean up some typos Cosmetic commit to clean up some of the annoying typos in qt-ui Signed-off-by: Henrik Brautaset Aronsen Signed-off-by: Dirk Hohndel --- qt-ui/divelistview.cpp | 6 +++--- qt-ui/maintab.cpp | 4 ++-- qt-ui/models.cpp | 16 ++++++++-------- qt-ui/models.h | 10 +++++----- qt-ui/profilegraphics.cpp | 4 ++-- 5 files changed, 20 insertions(+), 20 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp index a209118e0..d192d84aa 100644 --- a/qt-ui/divelistview.cpp +++ b/qt-ui/divelistview.cpp @@ -61,9 +61,9 @@ void DiveListView::selectionChanged(const QItemSelection& selected, const QItemS Q_FOREACH(const QModelIndex& index, deselected.indexes()) { const QAbstractItemModel *model = index.model(); struct dive *dive = (struct dive*) model->data(index, TreeItemDT::DIVE_ROLE).value(); - if (!dive) { // is's a trip! + if (!dive) { // it's a trip! if (model->rowCount(index)) { - expand(index); // leave this - even if it looks like it shouldn't be here. looks like I'v found a Qt bug. + expand(index); // leave this - even if it looks like it shouldn't be here. looks like I've found a Qt bug. // the subselection is removed, but the painting is not. this cleans the area. } } else if (!parents.contains(index.parent())) { @@ -74,7 +74,7 @@ void DiveListView::selectionChanged(const QItemSelection& selected, const QItemS Q_FOREACH(const QModelIndex& index, selected.indexes()) { const QAbstractItemModel *model = index.model(); struct dive *dive = (struct dive*) model->data(index, TreeItemDT::DIVE_ROLE).value(); - if (!dive) { // is's a trip! + if (!dive) { // it's a trip! if (model->rowCount(index)) { QItemSelection selection; selection.select(index.child(0,0), index.child(model->rowCount(index) -1 , 0)); diff --git a/qt-ui/maintab.cpp b/qt-ui/maintab.cpp index d8a966ae1..70cb3caea 100644 --- a/qt-ui/maintab.cpp +++ b/qt-ui/maintab.cpp @@ -90,7 +90,7 @@ void MainTab::updateDiveInfo(int dive) // it will be called whenever a new dive is selected // I'm already populating the 'notes' box // to show how it can be done. - // If you are unsure what's the name of anything, + // If you are unsure about the name of something, // open the file maintab.ui on the designer // click on the item and check its objectName, // the access is ui->objectName from here on. @@ -136,7 +136,7 @@ void MainTab::updateDiveInfo(int dive) ui->airPressureText->clear(); } /* statisticsTab*/ - /* we can access the stats_selection struct but how to we ensure the relevant dives are selected + /* we can access the stats_selection struct, but how do we ensure the relevant dives are selected * if we don't use the gtk widget to drive this? * Maybe call process_selected_dives? Or re-write to query our Qt list view. */ diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 68ab402cd..0b933aeb1 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -336,13 +336,13 @@ void TankInfoModel::update() TreeItemDT::~TreeItemDT() { - qDeleteAll(childs); + qDeleteAll(children); } int TreeItemDT::row() const { if (parent) - return parent->childs.indexOf(const_cast(this)); + return parent->children.indexOf(const_cast(this)); return 0; } @@ -637,7 +637,7 @@ const TreeItemDT* parentItem = (!parent.isValid()) ? rootItem : static_cast(parent.internalPointer()); - TreeItemDT* childItem = parentItem->childs[row]; + TreeItemDT* childItem = parentItem->children[row]; return (childItem) ? createIndex(row, column, childItem) : QModelIndex(); } @@ -665,7 +665,7 @@ int DiveTripModel::rowCount(const QModelIndex& parent) const else parentItem = static_cast(parent.internalPointer()); - int amount = parentItem->childs.count(); + int amount = parentItem->children.count(); return amount; } @@ -684,19 +684,19 @@ void DiveTripModel::setupModelData() if (!trip) { diveItem->parent = rootItem; - rootItem->childs.push_back(diveItem); + rootItem->children.push_back(diveItem); continue; } if (!trips.keys().contains(trip)) { TripItem* tripItem = new TripItem(); tripItem->trip = trip; tripItem->parent = rootItem; - tripItem->childs.push_back(diveItem); + tripItem->children.push_back(diveItem); trips[trip] = tripItem; - rootItem->childs.push_back(tripItem); + rootItem->children.push_back(tripItem); continue; } TripItem* tripItem = trips[trip]; - tripItem->childs.push_back(diveItem); + tripItem->children.push_back(diveItem); } } diff --git a/qt-ui/models.h b/qt-ui/models.h index 307cdf5c3..ac533fd71 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -14,7 +14,7 @@ #include "../divelist.h" /* Encapsulates the tank_info global variable - * to show on Qt`s Model View System.*/ + * to show on Qt's Model View System.*/ class TankInfoModel : public QAbstractTableModel { Q_OBJECT public: @@ -50,9 +50,9 @@ public: void clear(); void update(); private: - /* Since the dive doesn`t stores the number of cylinders that - * it has ( max 8 ) and since I don`t want to make a - * model-for-each-dive, let`s hack this here instead. */ + /* Since the dive doesn't stores the number of cylinders that + * it has (max 8) and since I don't want to make a + * model-for-each-dive, let's hack this here instead. */ QMap usedRows; }; @@ -94,7 +94,7 @@ public: virtual QVariant data ( int column, int role ) const; int row() const; - QList childs; + QList children; TreeItemDT *parent; }; diff --git a/qt-ui/profilegraphics.cpp b/qt-ui/profilegraphics.cpp index 571214f6f..8f25f1932 100644 --- a/qt-ui/profilegraphics.cpp +++ b/qt-ui/profilegraphics.cpp @@ -663,7 +663,7 @@ void ProfileGraphicsView::plot_temperature_text() /* don't print a temperature * if it's been less than 5min and less than a 2K change OR * if it's been less than 2min OR if the change from the - * last print is less than .4K (and therefore less than 1F */ + * last print is less than .4K (and therefore less than 1F) */ if (((sec < last + 300) && (abs(mkelvin - last_printed_temp) < 2000)) || (sec < last + 120) || (abs(mkelvin - last_printed_temp) < 400)) @@ -806,7 +806,7 @@ void ProfileGraphicsView::plot_one_event(struct event *ev) int i, depth = 0; struct plot_info *pi = &gc.pi; - /* is plotting this event disabled? */ + /* is plotting of this event disabled? */ if (ev->name) { for (i = 0; i < evn_used; i++) { if (! strcmp(ev->name, ev_namelist[i].ev_name)) { -- cgit v1.2.3-70-g09d2 From 5868b37e6bde1eaa6da09aac5e269557d94d7641 Mon Sep 17 00:00:00 2001 From: Henrik Brautaset Aronsen Date: Tue, 14 May 2013 09:45:01 +0200 Subject: Fix inaccurate weight and temperature display in dive list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A nonexisting temperature (mkelvin==0) was displayed as -273°C. Weight was always displayed with an extra 500 grams/0.5 lbs. Signed-off-by: Henrik Brautaset Aronsen Signed-off-by: Dirk Hohndel --- qt-ui/models.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 0b933aeb1..4c45f1489 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -540,6 +540,9 @@ QString DiveItem::displayTemperature() const { QString str; + if (!dive->watertemp.mkelvin) + return str; + if (get_units()->temperature == units::CELSIUS) str = QString::number(mkelvin_to_C(dive->watertemp.mkelvin), 'f', 1); else @@ -567,9 +570,9 @@ QString DiveItem::displayWeight() const if (get_units()->weight == units::KG) { int gr = weight() % 1000; int kg = weight() / 1000; - str = QString("%1.%2").arg(kg).arg((unsigned)(gr + 500) / 100); + str = QString("%1.%2").arg(kg).arg((unsigned)(gr) / 100); } else { - str = QString("%1").arg((unsigned)(grams_to_lbs(weight()) + 0.5)); + str = QString("%1").arg((unsigned)(grams_to_lbs(weight()))); } return str; -- cgit v1.2.3-70-g09d2 From a55a2e5d88c2e822f64e36b96910632b37ba8d6d Mon Sep 17 00:00:00 2001 From: Henrik Brautaset Aronsen Date: Tue, 14 May 2013 13:53:07 +0200 Subject: Don't add half a kilo/pound when adding weights The weight management widget added 500 grams / 0.5 lbs when a new entry was added. Signed-off-by: Henrik Brautaset Aronsen Signed-off-by: Dirk Hohndel --- qt-ui/models.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'qt-ui/models.cpp') diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 4c45f1489..9bc8db0bb 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -167,9 +167,9 @@ QVariant WeightModel::data(const QModelIndex& index, int role) const if (get_units()->weight == units::KG) { int gr = ws->weight.grams % 1000; int kg = ws->weight.grams / 1000; - ret = QString("%1.%2").arg(kg).arg((unsigned)(gr + 500) / 100); + ret = QString("%1.%2").arg(kg).arg((unsigned) gr / 100); } else { - ret = QString("%1").arg((unsigned)(grams_to_lbs(ws->weight.grams) + 0.5)); + ret = QString("%1").arg((unsigned)(grams_to_lbs(ws->weight.grams))); } break; } -- cgit v1.2.3-70-g09d2