Skip to content

Commit

Permalink
Merge pull request #9 from BreeDurbin/item-delegate
Browse files Browse the repository at this point in the history
added CStyleItemDelegate
  • Loading branch information
BreeDurbin authored Apr 10, 2024
2 parents 37cb1d7 + af9ba60 commit 3c28ddf
Show file tree
Hide file tree
Showing 17 changed files with 150 additions and 55 deletions.
Binary file added docs/mock/Pasted image 20240320003350.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
6 changes: 4 additions & 2 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ set(HEADER_FILES
#component
${CMAKE_CURRENT_SOURCE_DIR}/component/include/cpushbutton.hpp
${CMAKE_CURRENT_SOURCE_DIR}/component/include/ccombobox.hpp
${CMAKE_CURRENT_SOURCE_DIR}/component/include/clistview.hpp
${CMAKE_CURRENT_SOURCE_DIR}/component/include/ctableview.hpp
${CMAKE_CURRENT_SOURCE_DIR}/component/include/ctabwidget.hpp
${CMAKE_CURRENT_SOURCE_DIR}/component/include/clabel.hpp
${CMAKE_CURRENT_SOURCE_DIR}/component/include/cstyleditemdelegate.hpp
#style
${CMAKE_CURRENT_SOURCE_DIR}/style/include/colorrepository.hpp
${CMAKE_CURRENT_SOURCE_DIR}/style/include/cstyle.hpp
Expand All @@ -31,9 +32,10 @@ set(SOURCE_FILES
#component
${CMAKE_CURRENT_SOURCE_DIR}/component/cpushbutton.cpp
${CMAKE_CURRENT_SOURCE_DIR}/component/ccombobox.cpp
${CMAKE_CURRENT_SOURCE_DIR}/component/clistview.cpp
${CMAKE_CURRENT_SOURCE_DIR}/component/ctableview.cpp
${CMAKE_CURRENT_SOURCE_DIR}/component/ctabwidget.cpp
${CMAKE_CURRENT_SOURCE_DIR}/component/clabel.cpp
${CMAKE_CURRENT_SOURCE_DIR}/component/cstyleditemdelegate.cpp
#style
${CMAKE_CURRENT_SOURCE_DIR}/style/colorrepository.cpp
${CMAKE_CURRENT_SOURCE_DIR}/style/cstyle.cpp
Expand Down
8 changes: 0 additions & 8 deletions lib/component/clistview.cpp

This file was deleted.

16 changes: 16 additions & 0 deletions lib/component/cstyleditemdelegate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "include/cstyleditemdelegate.hpp"


CStyledItemDelegate::CStyledItemDelegate(QObject *parent) : QStyledItemDelegate(parent)
{
}

void CStyledItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyledItemDelegate::paint(painter, option, index);
}

QSize CStyledItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
return QStyledItemDelegate::sizeHint(option, index);
}
17 changes: 17 additions & 0 deletions lib/component/ctableview.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "include/ctableview.hpp"

#include <QHeaderView>

CTableView::CTableView(const QString text, QWidget *parent){
this->viewport()->setAutoFillBackground(false);
this->setAutoFillBackground(false);
this->setFrameStyle(QFrame::NoFrame);
this->horizontalHeader()->hide();
this->verticalHeader()->hide();
this->setShowGrid(false);
this->setColumnWidth(0, 200);

}

CTableView::~CTableView(){};

16 changes: 0 additions & 16 deletions lib/component/include/clistview.hpp

This file was deleted.

13 changes: 13 additions & 0 deletions lib/component/include/cstyleditemdelegate.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include<QStyledItemDelegate>

class CStyledItemDelegate : public QStyledItemDelegate
{
Q_OBJECT

public:

CStyledItemDelegate(QObject *parent = nullptr);

void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
};
16 changes: 16 additions & 0 deletions lib/component/include/ctableview.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <QTableView>

class CTableView : public QTableView
{
Q_OBJECT

public:
explicit CTableView(const QString text = "", QWidget *parent = nullptr);
~CTableView();

private:
// Add any private member variables or functions here

};
27 changes: 16 additions & 11 deletions lib/style/colorrepository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@

static bool s_darkMode = true;

QColor m_lightGold(){ return QColor(197,157,98); }
QColor m_gold(){ return QColor(255,215,0); }
QColor m_darkGold(){ return QColor(184,134,11); }
QColor m_lightBlue(){ return QColor(0,0,255); }
QColor m_blue(){ return QColor(0,0,128); }
QColor m_darkBlue(){ return QColor(11,35,59); }

QPalette ColorRepository::standardPalette()
{
QPalette pal;
Expand Down Expand Up @@ -34,39 +41,37 @@ void ColorRepository::setDarkMode(bool dark)

QColor ColorRepository::windowBackground()
{
return s_darkMode ? QColor(235, 180, 15)
: QColor(12, 51, 194);
return m_darkBlue();
}

QColor ColorRepository::baseBackground()
{
return s_darkMode ? QColor(235, 180, 15)
: QColor(12, 51, 194);
return m_darkBlue();
}

QColor ColorRepository::text()
{
return s_darkMode ? QColor(0xa5, 0xa5, 0xa5) : QColor(0x25, 0x25, 0x25);
return m_lightGold();
}

QColor ColorRepository::pressedTextColor()
{
return QColor(12, 51, 194);
return m_darkGold();
}

QColor ColorRepository::hoverTextColor()
{
return QColor(235, 180, 15);
return m_gold();
}

QColor ColorRepository::pressedOutlineColor()
{
return QColor(0x32, 0x2d, 0x35);
return m_darkGold();
}

QColor ColorRepository::buttonOutlineColor()
{
return s_darkMode ? QColor(0xa5, 0xa5, 0xa5) : QColor(0x25, 0x25, 0x25);
return m_lightGold();
}

QBrush ColorRepository::hoverOutlineBrush(const QRect &rect)
Expand All @@ -91,7 +96,7 @@ QBrush ColorRepository::hoverOutlineBrush(const QRect &rect)

QColor ColorRepository::buttonPressedBackground()
{
return s_darkMode ? QColor(0x17, 0x17, 0x17) : QColor(0xf8, 0xf7, 0xf8);
return m_lightGold();
}

QColor ColorRepository::buttonHoveredBackground()
Expand All @@ -103,5 +108,5 @@ QColor ColorRepository::buttonHoveredBackground()

QColor ColorRepository::buttonBackground()
{
return s_darkMode ? QColor(0x21, 0x1f, 0x22, 0xa7) : QColor(0xf5, 0xf4, 0xf5) /* TODO with opacity = ? */;
return m_darkBlue();
}
1 change: 0 additions & 1 deletion lib/style/include/colorrepository.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ namespace ColorRepository
QColor buttonPressedBackground();
QColor buttonHoveredBackground();
QColor buttonBackground();

}
4 changes: 2 additions & 2 deletions src/component/include/creaturerepository.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include <QListView>
#include <QTableView>

class CreatureRepository : public QListView
class CreatureRepository : public QTableView
{
Q_OBJECT

Expand Down
Binary file added src/icons/initiative.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 23 additions & 6 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

#include <clabel.hpp>
#include <cpushbutton.hpp>
#include <clistview.hpp>
#include <ctableview.hpp>
#include <creaturemodel.hpp>
#include <ctabwidget.hpp>
#include <cstyleditemdelegate.hpp>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
Expand Down Expand Up @@ -62,7 +63,9 @@ void MainWindow::initializeInitiative(QHBoxLayout* outer){
auto banner = new CLabel("Initiative Tracker");
inner->addWidget(banner);

CListView *initiativeRepository = new CListView();
auto *initiativeRepository = new CTableView();
auto itemDelegate = new CStyledItemDelegate();
initiativeRepository->setItemDelegate(itemDelegate);
auto initiativeList = new CreatureModel();
initiativeRepository->setModel(initiativeList);
inner->addWidget(initiativeRepository);
Expand Down Expand Up @@ -92,17 +95,31 @@ void MainWindow::initializeCreatureRepository(QHBoxLayout* outer){
auto banner = new CLabel("Character Repository");
creatureRepositoryBannerLayout->addWidget(banner);

CPushButton *addPcButton = new CPushButton("Add Player Character");
creatureRepositoryBannerLayout->addWidget(addPcButton);
auto *addCreatureButton = new CPushButton("+");
creatureRepositoryBannerLayout->addWidget(addCreatureButton);
auto *removeCreatureButton = new CPushButton("-");
creatureRepositoryBannerLayout->addWidget(removeCreatureButton);

auto creatureRepository = new CListView();
auto creatureRepository = new CTableView();
auto itemDelegate = new CStyledItemDelegate();
creatureRepository->setItemDelegate(itemDelegate);
auto pcList = new CreatureModel();
creatureRepository->setModel(pcList);
inner->addWidget(creatureRepository);
creatureRepository->show();

//connections
connect(addPcButton, &QPushButton::clicked, [pcList](){
connect(addCreatureButton, &QPushButton::clicked, [pcList](){
CreatureItem temp;
temp.name = "New PC";
temp.type = "PC";
temp.level = 1;
temp.initiative = 0;
pcList->addRows(temp);
});

//connections
connect(removeCreatureButton, &QPushButton::clicked, [pcList](){
CreatureItem temp;
temp.name = "New PC";
temp.type = "PC";
Expand Down
46 changes: 39 additions & 7 deletions src/model/creaturemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <QColor>
#include <QFont>
#include <QIcon>
#include "colorrepository.hpp"


CreatureModel::CreatureModel(QObject *parent){
Expand Down Expand Up @@ -36,6 +37,10 @@ int CreatureModel::rowCount(const QModelIndex &parent) const{
return !parent.isValid() ? creatures.size() : 0;
}

int CreatureModel::columnCount(const QModelIndex &parent) const{
return !parent.isValid() ? 4 : 0;
}

QVariant CreatureModel::data(const QModelIndex &index, int role) const{
if (!index.isValid())
return QVariant();
Expand All @@ -44,17 +49,44 @@ QVariant CreatureModel::data(const QModelIndex &index, int role) const{
case Qt::TextAlignmentRole:
return Qt::AlignCenter;
case Qt::FontRole:
return QFont("Helvetica", 16, QFont::Bold);
return QFont("Helvetica", 20, QFont::Bold);
case Qt::BackgroundRole:
return QBrush(QColor(60, 60, 60));
return QBrush(ColorRepository::baseBackground());
case Qt::ForegroundRole:
return QBrush(QColor(255, 255, 255));
return QBrush(ColorRepository::text());
case Qt::DecorationRole:
return QIcon(":/icons/monster.png");
if(index.column() == 0)
return QVariant();
else if(index.column() == 1)
return QIcon(":/icons/monster.png");
else if(index.column() == 2)
return QVariant();
else if(index.column() == 3)
return QIcon(":/icons/initiative.png");
else
return QVariant();
case Qt::DisplayRole:
return QString(creatures[index.row()].name + " " + creatures[index.row()].type + " " + QString::number(creatures[index.row()].level) + " " + QString::number(creatures[index.row()].initiative));
if(index.column() == 0)
return QString(creatures[index.row()].name);
else if(index.column() == 1)
return QString(creatures[index.row()].type);
else if(index.column() == 2)
return QString::number(creatures[index.row()].level);
else if(index.column() == 3)
return QString::number(creatures[index.row()].initiative);
else
return QVariant();
case Qt::EditRole:
return QString(creatures[index.row()].name + " " + creatures[index.row()].type + " " + QString::number(creatures[index.row()].level) + " " + QString::number(creatures[index.row()].initiative));
if(index.column() == 0)
return QString(creatures[index.row()].name);
else if(index.column() == 1)
return QString(creatures[index.row()].type);
else if(index.column() == 2)
return QString::number(creatures[index.row()].level);
else if(index.column() == 3)
return QString::number(creatures[index.row()].initiative);
else
return QVariant();
default:
return QVariant();
}
Expand Down Expand Up @@ -86,7 +118,7 @@ bool CreatureModel::setData(const QModelIndex &index, const QVariant &value, int
}

Qt::ItemFlags CreatureModel::flags(const QModelIndex &index) const{
return index.isValid() ? Qt::ItemIsEditable | QAbstractListModel::flags(index) : Qt::NoItemFlags;
return index.isValid() ? Qt::ItemIsEditable | QAbstractTableModel::flags(index) : Qt::NoItemFlags;
}

void CreatureModel::addRows(CreatureItem &creatureItem, const QModelIndex &parent){
Expand Down
5 changes: 3 additions & 2 deletions src/model/include/creaturemodel.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <QAbstractListModel>
#include <QAbstractTableModel>


struct CreatureItem // nested class
Expand All @@ -9,13 +9,14 @@ struct CreatureItem // nested class
int initiative;
};

class CreatureModel : public QAbstractListModel{
class CreatureModel : public QAbstractTableModel{

Q_OBJECT

public:
CreatureModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent) const override;
int columnCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &index, int role) const override;
QHash<int, QByteArray> roleNames() const override;
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
Expand Down
1 change: 1 addition & 0 deletions src/resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
<file>images/cyrus_logo.png</file>
<file>images/cyrus_background.png</file>
<file>icons/monster.png</file>
<file>icons/initiative.png</file>
</qresource>
</RCC>

0 comments on commit 3c28ddf

Please sign in to comment.