Skip to content

Commit

Permalink
Format all code using clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
steveire committed Mar 19, 2016
1 parent e4e0b73 commit 00a053f
Show file tree
Hide file tree
Showing 239 changed files with 14,624 additions and 12,658 deletions.
110 changes: 60 additions & 50 deletions examples/books/bookdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@

#include "bookdelegate.h"

#include <QPainter>
#include <QMouseEvent>
#include <QPainter>
#include <QSpinBox>

BookDelegate::BookDelegate(QObject *parent)
Expand All @@ -51,77 +51,87 @@ BookDelegate::BookDelegate(QObject *parent)
}

void BookDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
const QModelIndex &index) const
{
if (index.column() != 5) {
QStyleOptionViewItemV3 opt = option;
opt.rect.adjust(0, 0, -1, -1); // since we draw the grid ourselves
QSqlRelationalDelegate::paint(painter, opt, index);
} else {
const QAbstractItemModel *model = index.model();
QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ?
(option.state & QStyle::State_Active) ? QPalette::Normal : QPalette::Inactive : QPalette::Disabled;
if (index.column() != 5) {
QStyleOptionViewItemV3 opt = option;
opt.rect.adjust(0, 0, -1, -1); // since we draw the grid ourselves
QSqlRelationalDelegate::paint(painter, opt, index);
} else {
const QAbstractItemModel *model = index.model();
QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled)
? (option.state & QStyle::State_Active)
? QPalette::Normal
: QPalette::Inactive
: QPalette::Disabled;

if (option.state & QStyle::State_Selected)
painter->fillRect(option.rect, option.palette.color(cg, QPalette::Highlight));
if (option.state & QStyle::State_Selected)
painter->fillRect(option.rect,
option.palette.color(cg, QPalette::Highlight));

int rating = model->data(index, Qt::DisplayRole).toInt();
int width = star.width();
int height = star.height();
int x = option.rect.x();
int y = option.rect.y() + (option.rect.height() / 2) - (height / 2);
for (int i = 0; i < rating; ++i) {
painter->drawPixmap(x, y, star);
x += width;
}
drawFocus(painter, option, option.rect.adjusted(0, 0, -1, -1)); // since we draw the grid ourselves
int rating = model->data(index, Qt::DisplayRole).toInt();
int width = star.width();
int height = star.height();
int x = option.rect.x();
int y = option.rect.y() + (option.rect.height() / 2) - (height / 2);
for (int i = 0; i < rating; ++i) {
painter->drawPixmap(x, y, star);
x += width;
}
drawFocus(
painter, option,
option.rect.adjusted(0, 0, -1, -1)); // since we draw the grid ourselves
}

QPen pen = painter->pen();
painter->setPen(option.palette.color(QPalette::Mid));
painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
painter->setPen(pen);
QPen pen = painter->pen();
painter->setPen(option.palette.color(QPalette::Mid));
painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
painter->setPen(pen);
}

QSize BookDelegate::sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const
const QModelIndex &index) const
{
if (index.column() == 5)
return QSize(5 * star.width(), star.height()) + QSize(1, 1);
if (index.column() == 5)
return QSize(5 * star.width(), star.height()) + QSize(1, 1);

return QSqlRelationalDelegate::sizeHint(option, index) + QSize(1, 1); // since we draw the grid ourselves
return QSqlRelationalDelegate::sizeHint(option, index)
+ QSize(1, 1); // since we draw the grid ourselves
}

bool BookDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
const QStyleOptionViewItem &option,
const QModelIndex &index)
{
if (index.column() != 5)
return QSqlRelationalDelegate::editorEvent(event, model, option, index);
if (index.column() != 5)
return QSqlRelationalDelegate::editorEvent(event, model, option, index);

if (event->type() == QEvent::MouseButtonPress) {
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
int stars = qBound(0, int(0.7 + qreal(mouseEvent->pos().x()
- option.rect.x()) / star.width()), 5);
model->setData(index, QVariant(stars));
return false; //so that the selection can change
}
if (event->type() == QEvent::MouseButtonPress) {
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
int stars = qBound(
0, int(0.7
+ qreal(mouseEvent->pos().x() - option.rect.x()) / star.width()),
5);
model->setData(index, QVariant(stars));
return false; // so that the selection can change
}

return true;
return true;
}

QWidget *BookDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
QWidget *BookDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (index.column() != 4)
return QSqlRelationalDelegate::createEditor(parent, option, index);
if (index.column() != 4)
return QSqlRelationalDelegate::createEditor(parent, option, index);

// for editing the year, return a spinbox with a range from -1000 to 2100.
QSpinBox *sb = new QSpinBox(parent);
sb->setFrame(false);
sb->setMaximum(2100);
sb->setMinimum(-1000);
// for editing the year, return a spinbox with a range from -1000 to 2100.
QSpinBox *sb = new QSpinBox(parent);
sb->setFrame(false);
sb->setMaximum(2100);
sb->setMinimum(-1000);

return sb;
return sb;
}
21 changes: 11 additions & 10 deletions examples/books/bookdelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,23 @@ QT_FORWARD_DECLARE_CLASS(QPainter)
class BookDelegate : public QSqlRelationalDelegate
{
public:
BookDelegate(QObject *parent);
BookDelegate(QObject *parent);

void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const;

QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
QSize sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const;

bool editorEvent(QEvent *event, QAbstractItemModel *model,
const QStyleOptionViewItem &option,
const QModelIndex &index);
bool editorEvent(QEvent *event, QAbstractItemModel *model,
const QStyleOptionViewItem &option,
const QModelIndex &index);

QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const;

private:
QPixmap star;
QPixmap star;
};

#endif
Loading

0 comments on commit 00a053f

Please sign in to comment.