mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
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 <tcanabrava@kde.org>
This commit is contained in:
parent
1d0d42f861
commit
2f4d6bbe53
9 changed files with 115 additions and 59 deletions
2
Makefile
2
Makefile
|
@ -37,6 +37,7 @@ HEADERS = \
|
||||||
qt-ui/models.h \
|
qt-ui/models.h \
|
||||||
qt-ui/plotareascene.h \
|
qt-ui/plotareascene.h \
|
||||||
qt-ui/starwidget.h \
|
qt-ui/starwidget.h \
|
||||||
|
qt-ui/modeldelegates.h \
|
||||||
|
|
||||||
|
|
||||||
SOURCES = \
|
SOURCES = \
|
||||||
|
@ -75,6 +76,7 @@ SOURCES = \
|
||||||
qt-ui/models.cpp \
|
qt-ui/models.cpp \
|
||||||
qt-ui/plotareascene.cpp \
|
qt-ui/plotareascene.cpp \
|
||||||
qt-ui/starwidget.cpp \
|
qt-ui/starwidget.cpp \
|
||||||
|
qt-ui/modeldelegates.cpp \
|
||||||
$(RESFILE)
|
$(RESFILE)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,8 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
#include "divelistview.h"
|
#include "divelistview.h"
|
||||||
|
#include "models.h"
|
||||||
|
#include "modeldelegates.h"
|
||||||
|
|
||||||
DiveListView::DiveListView(QWidget *parent) : QTreeView(parent)
|
DiveListView::DiveListView(QWidget *parent) : QTreeView(parent)
|
||||||
{
|
{
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
#include "../dive.h"
|
#include "../dive.h"
|
||||||
#include "../divelist.h"
|
#include "../divelist.h"
|
||||||
#include "../pref.h"
|
#include "../pref.h"
|
||||||
|
#include "modeldelegates.h"
|
||||||
|
|
||||||
MainWindow::MainWindow() : ui(new Ui::MainWindow()),
|
MainWindow::MainWindow() : ui(new Ui::MainWindow()),
|
||||||
model(new DiveTripModel(this)),
|
model(new DiveTripModel(this)),
|
||||||
|
@ -69,6 +69,7 @@ void MainWindow::on_actionOpen_triggered()
|
||||||
model->deleteLater();
|
model->deleteLater();
|
||||||
model = new DiveTripModel(this);
|
model = new DiveTripModel(this);
|
||||||
sortModel->setSourceModel(model);
|
sortModel->setSourceModel(model);
|
||||||
|
ui->ListWidget->setItemDelegateForColumn(DiveTripModel::RATING, new StarWidgetsDelegate());
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::on_actionSave_triggered()
|
void MainWindow::on_actionSave_triggered()
|
||||||
|
|
36
qt-ui/modeldelegates.cpp
Normal file
36
qt-ui/modeldelegates.cpp
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
#include "modeldelegates.h"
|
||||||
|
#include "../dive.h"
|
||||||
|
#include "../divelist.h"
|
||||||
|
#include "starwidget.h"
|
||||||
|
#include "models.h"
|
||||||
|
|
||||||
|
#include <QtDebug>
|
||||||
|
#include <QPainter>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
12
qt-ui/modeldelegates.h
Normal file
12
qt-ui/modeldelegates.h
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
#ifndef MODELDELEGATES_H
|
||||||
|
#define MODELDELEGATES_H
|
||||||
|
|
||||||
|
#include <QAbstractItemDelegate>
|
||||||
|
|
||||||
|
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
|
|
@ -5,9 +5,6 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
#include "models.h"
|
#include "models.h"
|
||||||
#include "../dive.h"
|
|
||||||
#include "../divelist.h"
|
|
||||||
|
|
||||||
#include <QtDebug>
|
#include <QtDebug>
|
||||||
|
|
||||||
extern struct tank_info tank_info[100];
|
extern struct tank_info tank_info[100];
|
||||||
|
@ -284,6 +281,7 @@ void TankInfoModel::update()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*! A DiveItem for use with a DiveTripModel
|
/*! A DiveItem for use with a DiveTripModel
|
||||||
*
|
*
|
||||||
* A simple class which wraps basic stats for a dive (e.g. duration, depth) and
|
* A simple class which wraps basic stats for a dive (e.g. duration, depth) and
|
||||||
|
@ -314,6 +312,8 @@ public:
|
||||||
return tw.grams;
|
return tw.grams;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int diveRating() const { return dive->rating; }
|
||||||
|
|
||||||
QString displayDuration() const;
|
QString displayDuration() const;
|
||||||
QString displayDepth() const;
|
QString displayDepth() const;
|
||||||
QString displayTemperature() const;
|
QString displayTemperature() const;
|
||||||
|
@ -335,6 +335,7 @@ private:
|
||||||
QList <DiveItem*> childlist;
|
QList <DiveItem*> childlist;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
DiveItem::DiveItem(struct dive *d, DiveItem *p):
|
DiveItem::DiveItem(struct dive *d, DiveItem *p):
|
||||||
dive(d),
|
dive(d),
|
||||||
parentItem(p)
|
parentItem(p)
|
||||||
|
@ -490,6 +491,16 @@ QVariant DiveTripModel::data(const QModelIndex &index, int role) const
|
||||||
case LOCATION:
|
case LOCATION:
|
||||||
retVal = item->diveLocation();
|
retVal = item->diveLocation();
|
||||||
break;
|
break;
|
||||||
|
case RATING:
|
||||||
|
retVal = item->diveRating();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (role == DelegatesRole){
|
||||||
|
switch(index.column()){
|
||||||
|
case RATING:
|
||||||
|
retVal = item->diveRating();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return retVal;
|
return retVal;
|
||||||
|
@ -569,8 +580,6 @@ int DiveTripModel::rowCount(const QModelIndex &parent) const
|
||||||
return item ? item->children().count() : 0;
|
return item ? item->children().count() : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int DiveTripModel::columnCount(const QModelIndex &parent) const
|
int DiveTripModel::columnCount(const QModelIndex &parent) const
|
||||||
{
|
{
|
||||||
return parent.isValid() && parent.column() != 0 ? 0 : COLUMNS;
|
return parent.isValid() && parent.column() != 0 ? 0 : COLUMNS;
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
#include <QAbstractTableModel>
|
#include <QAbstractTableModel>
|
||||||
#include "../dive.h"
|
#include "../dive.h"
|
||||||
|
#include "../divelist.h"
|
||||||
/* Encapsulates the tank_info global variable
|
/* 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 {
|
class TankInfoModel : public QAbstractTableModel {
|
||||||
|
@ -74,11 +74,12 @@ private:
|
||||||
/*! An AbstractItemModel for recording dive trip information such as a list of dives.
|
/*! 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
|
class DiveTripModel : public QAbstractItemModel
|
||||||
{
|
{
|
||||||
public:
|
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, COLUMNS };
|
||||||
|
enum { DelegatesRole = Qt::UserRole };
|
||||||
|
|
||||||
DiveTripModel(QObject *parent = 0);
|
DiveTripModel(QObject *parent = 0);
|
||||||
|
|
||||||
|
|
|
@ -5,32 +5,29 @@
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QMouseEvent>
|
#include <QMouseEvent>
|
||||||
|
|
||||||
|
QPixmap* StarWidget::activeStar = 0;
|
||||||
|
QPixmap* StarWidget::inactiveStar = 0;
|
||||||
|
|
||||||
|
QPixmap StarWidget::starActive()
|
||||||
|
{
|
||||||
|
return (*activeStar);
|
||||||
|
}
|
||||||
|
|
||||||
|
QPixmap StarWidget::starInactive()
|
||||||
|
{
|
||||||
|
return (*inactiveStar);
|
||||||
|
}
|
||||||
|
|
||||||
int StarWidget::currentStars() const
|
int StarWidget::currentStars() const
|
||||||
{
|
{
|
||||||
return current;
|
return current;
|
||||||
}
|
}
|
||||||
|
|
||||||
void StarWidget::enableHalfStars(bool enabled)
|
|
||||||
{
|
|
||||||
halfStar = enabled;
|
|
||||||
update();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool StarWidget::halfStarsEnabled() const
|
|
||||||
{
|
|
||||||
return halfStar;
|
|
||||||
}
|
|
||||||
|
|
||||||
int StarWidget::maxStars() const
|
|
||||||
{
|
|
||||||
return stars;
|
|
||||||
}
|
|
||||||
|
|
||||||
void StarWidget::mouseReleaseEvent(QMouseEvent* event)
|
void StarWidget::mouseReleaseEvent(QMouseEvent* event)
|
||||||
{
|
{
|
||||||
int starClicked = event->pos().x() / IMG_SIZE + 1;
|
int starClicked = event->pos().x() / IMG_SIZE + 1;
|
||||||
if (starClicked > stars)
|
if (starClicked > TOTALSTARS)
|
||||||
starClicked = stars;
|
starClicked = TOTALSTARS;
|
||||||
|
|
||||||
if (current == starClicked)
|
if (current == starClicked)
|
||||||
current -= 1;
|
current -= 1;
|
||||||
|
@ -45,10 +42,10 @@ void StarWidget::paintEvent(QPaintEvent* event)
|
||||||
QPainter p(this);
|
QPainter p(this);
|
||||||
|
|
||||||
for(int i = 0; i < current; i++)
|
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++)
|
for(int i = current; i < TOTALSTARS; i++)
|
||||||
p.drawPixmap(i * IMG_SIZE + SPACING, 0, inactiveStar);
|
p.drawPixmap(i * IMG_SIZE + SPACING, 0, starInactive());
|
||||||
}
|
}
|
||||||
|
|
||||||
void StarWidget::setCurrentStars(int value)
|
void StarWidget::setCurrentStars(int value)
|
||||||
|
@ -58,18 +55,12 @@ void StarWidget::setCurrentStars(int value)
|
||||||
Q_EMIT valueChanged(current);
|
Q_EMIT valueChanged(current);
|
||||||
}
|
}
|
||||||
|
|
||||||
void StarWidget::setMaximumStars(int maximum)
|
|
||||||
{
|
|
||||||
stars = maximum;
|
|
||||||
update();
|
|
||||||
}
|
|
||||||
|
|
||||||
StarWidget::StarWidget(QWidget* parent, Qt::WindowFlags f):
|
StarWidget::StarWidget(QWidget* parent, Qt::WindowFlags f):
|
||||||
QWidget(parent, f),
|
QWidget(parent, f),
|
||||||
stars(5),
|
current(0)
|
||||||
current(0),
|
|
||||||
halfStar(false)
|
|
||||||
{
|
{
|
||||||
|
if(!activeStar){
|
||||||
|
activeStar = new QPixmap();
|
||||||
QSvgRenderer render(QString(":star"));
|
QSvgRenderer render(QString(":star"));
|
||||||
QPixmap renderedStar(IMG_SIZE, IMG_SIZE);
|
QPixmap renderedStar(IMG_SIZE, IMG_SIZE);
|
||||||
|
|
||||||
|
@ -77,8 +68,12 @@ StarWidget::StarWidget(QWidget* parent, Qt::WindowFlags f):
|
||||||
QPainter painter(&renderedStar);
|
QPainter painter(&renderedStar);
|
||||||
|
|
||||||
render.render(&painter, QRectF(0, 0, IMG_SIZE, IMG_SIZE));
|
render.render(&painter, QRectF(0, 0, IMG_SIZE, IMG_SIZE));
|
||||||
activeStar = renderedStar;
|
(*activeStar) = renderedStar;
|
||||||
inactiveStar = grayImage(&renderedStar);
|
}
|
||||||
|
if(!inactiveStar){
|
||||||
|
inactiveStar = new QPixmap();
|
||||||
|
(*inactiveStar) = grayImage(activeStar);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QPixmap StarWidget::grayImage(QPixmap* coloredImg)
|
QPixmap StarWidget::grayImage(QPixmap* coloredImg)
|
||||||
|
@ -86,17 +81,20 @@ QPixmap StarWidget::grayImage(QPixmap* coloredImg)
|
||||||
QImage img = coloredImg->toImage();
|
QImage img = coloredImg->toImage();
|
||||||
for (int i = 0; i < img.width(); ++i) {
|
for (int i = 0; i < img.width(); ++i) {
|
||||||
for (int j = 0; j < img.height(); ++j) {
|
for (int j = 0; j < img.height(); ++j) {
|
||||||
QRgb col = img.pixel(i, j);
|
QRgb rgb = img.pixel(i, j);
|
||||||
if (!col)
|
if (!rgb)
|
||||||
continue;
|
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));
|
img.setPixel(i, j, qRgb(gray, gray, gray));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return QPixmap::fromImage(img);
|
return QPixmap::fromImage(img);
|
||||||
}
|
}
|
||||||
|
|
||||||
QSize StarWidget::sizeHint() const
|
QSize StarWidget::sizeHint() const
|
||||||
{
|
{
|
||||||
return QSize(IMG_SIZE * stars + SPACING * (stars-1), IMG_SIZE);
|
return QSize(IMG_SIZE * TOTALSTARS + SPACING * (TOTALSTARS-1), IMG_SIZE);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,39 +3,34 @@
|
||||||
|
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
|
||||||
|
enum StarConfig {SPACING = 2, IMG_SIZE = 16, TOTALSTARS = 5};
|
||||||
|
|
||||||
class StarWidget : public QWidget
|
class StarWidget : public QWidget
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit StarWidget(QWidget* parent = 0, Qt::WindowFlags f = 0);
|
explicit StarWidget(QWidget* parent = 0, Qt::WindowFlags f = 0);
|
||||||
|
|
||||||
int maxStars() const;
|
|
||||||
int currentStars() const;
|
int currentStars() const;
|
||||||
bool halfStarsEnabled() const;
|
|
||||||
|
|
||||||
/*reimp*/ QSize sizeHint() const;
|
/*reimp*/ QSize sizeHint() const;
|
||||||
|
|
||||||
enum {SPACING = 2, IMG_SIZE = 16};
|
static QPixmap starActive();
|
||||||
|
static QPixmap starInactive();
|
||||||
|
|
||||||
Q_SIGNALS:
|
Q_SIGNALS:
|
||||||
void valueChanged(int stars);
|
void valueChanged(int stars);
|
||||||
|
|
||||||
public Q_SLOTS:
|
public Q_SLOTS:
|
||||||
void setCurrentStars(int value);
|
void setCurrentStars(int value);
|
||||||
void setMaximumStars(int maximum);
|
|
||||||
void enableHalfStars(bool enabled);
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/*reimp*/ void mouseReleaseEvent(QMouseEvent* );
|
/*reimp*/ void mouseReleaseEvent(QMouseEvent* );
|
||||||
/*reimp*/ void paintEvent(QPaintEvent* );
|
/*reimp*/ void paintEvent(QPaintEvent* );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int stars;
|
|
||||||
int current;
|
int current;
|
||||||
bool halfStar;
|
static QPixmap* activeStar;
|
||||||
QPixmap activeStar;
|
static QPixmap* inactiveStar;
|
||||||
QPixmap inactiveStar;
|
|
||||||
QPixmap grayImage(QPixmap *coloredImg);
|
QPixmap grayImage(QPixmap *coloredImg);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue