mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
Merge branch 'RenderStarsOnTable' of https://github.com/tcanabrava/subsurface into Qt
This commit is contained in:
commit
4179885da7
9 changed files with 115 additions and 59 deletions
2
Makefile
2
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)
|
||||
|
||||
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
*
|
||||
*/
|
||||
#include "divelistview.h"
|
||||
#include "models.h"
|
||||
#include "modeldelegates.h"
|
||||
|
||||
DiveListView::DiveListView(QWidget *parent) : QTreeView(parent)
|
||||
{
|
||||
|
|
|
@ -22,7 +22,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()
|
||||
|
|
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 "../dive.h"
|
||||
#include "../divelist.h"
|
||||
|
||||
#include <QtDebug>
|
||||
|
||||
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 <DiveItem*> 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;
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
#include <QAbstractTableModel>
|
||||
#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);
|
||||
|
||||
|
|
|
@ -5,32 +5,29 @@
|
|||
#include <QDebug>
|
||||
#include <QMouseEvent>
|
||||
|
||||
QPixmap* StarWidget::activeStar = 0;
|
||||
QPixmap* StarWidget::inactiveStar = 0;
|
||||
|
||||
QPixmap StarWidget::starActive()
|
||||
{
|
||||
return (*activeStar);
|
||||
}
|
||||
|
||||
QPixmap StarWidget::starInactive()
|
||||
{
|
||||
return (*inactiveStar);
|
||||
}
|
||||
|
||||
int StarWidget::currentStars() const
|
||||
{
|
||||
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)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -3,39 +3,34 @@
|
|||
|
||||
#include <QWidget>
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue