diff --git a/CMakeLists.txt b/CMakeLists.txt index e0e6566..2d7e97d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets) @@ -25,7 +25,7 @@ if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) MANUAL_FINALIZATION ${PROJECT_SOURCES} tiercard.hpp tiercard.cpp - flowlayout.cpp flowlayout.h flowlayout.pro + flowlayout.cpp flowlayout.h tiercardlayout.hpp tiercardlayout.cpp aspectratiopixmaplabel.hpp aspectratiopixmaplabel.cpp fullsizelayout.hpp fullsizelayout.cpp @@ -35,6 +35,8 @@ if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) utils.hpp rowholder.hpp rowholder.cpp settings.hpp settings.cpp + invalididexception.hpp invalididexception.cpp + tierplaceholder.hpp tierplaceholder.cpp ) # Define target properties for Android with Qt 6 as: diff --git a/flowlayout.cpp b/flowlayout.cpp index 9ce3c4e..c570647 100644 --- a/flowlayout.cpp +++ b/flowlayout.cpp @@ -5,7 +5,7 @@ #include "flowlayout.h" //! [1] -FlowLayout::FlowLayout(QWidget *parent, int margin, int hSpacing, int vSpacing) +FlowLayout::FlowLayout(QWidget* parent, int margin, int hSpacing, int vSpacing) : QLayout(parent), m_hSpace(hSpacing), m_vSpace(vSpacing) { setContentsMargins(margin, margin, margin, margin); @@ -102,6 +102,8 @@ QSize FlowLayout::minimumSize() const const QMargins margins = contentsMargins(); size += QSize(margins.left() + margins.right(), margins.top() + margins.bottom()); + if (size.height() < minimumHeight()) + size.rheight() = minimumHeight(); return size; } @@ -143,12 +145,41 @@ int FlowLayout::doLayout(const QRect &rect, bool testOnly) const int FlowLayout::smartSpacing(QStyle::PixelMetric pm) const { QObject* parent = this->parent(); - if (!parent) { + if (!parent) + { return -1; - } else if (parent->isWidgetType()) { - QWidget* pw = static_cast(parent); + } + else if (parent->isWidgetType()) + { + QWidget* pw = static_cast(parent); return pw->style()->pixelMetric(pm, nullptr, pw); - } else { + } + else + { return static_cast(parent)->spacing(); } } + +void FlowLayout::setIndex(QWidget* wid, int index) +{ + if (itemAt(index)->widget() == wid) + return; + wid->setParent(nullptr); + addWidget(wid); + if (index < 0) + return; + for (int i = count()-1; i > index; i--) + { + itemList.swapItemsAt(i-1, i); + } +} + +void FlowLayout::setMinimumHeight(int minh) +{ + _minimumHeight = minh; +} + +int FlowLayout::minimumHeight() const +{ + return _minimumHeight; +} diff --git a/flowlayout.h b/flowlayout.h index 3c5291b..9bf0c6f 100644 --- a/flowlayout.h +++ b/flowlayout.h @@ -7,7 +7,7 @@ #include #include #include -//! [0] + class FlowLayout : public QLayout { public: @@ -22,20 +22,22 @@ public: bool hasHeightForWidth() const override; int heightForWidth(int) const override; int count() const override; - QLayoutItem *itemAt(int index) const override; + QLayoutItem* itemAt(int index) const override; QSize minimumSize() const override; void setGeometry(const QRect &rect) override; QSize sizeHint() const override; - QLayoutItem *takeAt(int index) override; + QLayoutItem* takeAt(int index) override; + void setIndex(QWidget* wid, int index); + void setMinimumHeight(int minh); + int minimumHeight() const; private: int doLayout(const QRect &rect, bool testOnly) const; int smartSpacing(QStyle::PixelMetric pm) const; - - QList itemList; + int _minimumHeight = 0; + QList itemList; int m_hSpace; int m_vSpace; }; -//! [0] #endif // FLOWLAYOUT_H diff --git a/flowlayout.pro b/flowlayout.pro deleted file mode 100644 index a0cfd40..0000000 --- a/flowlayout.pro +++ /dev/null @@ -1,11 +0,0 @@ -QT += widgets - -HEADERS = flowlayout.h \ - window.h -SOURCES = flowlayout.cpp \ - main.cpp \ - window.cpp - -# install -target.path = $$[QT_INSTALL_EXAMPLES]/widgets/layouts/flowlayout -INSTALLS += target diff --git a/fullsizelayout.cpp b/fullsizelayout.cpp index af9f6f2..7ee37a5 100644 --- a/fullsizelayout.cpp +++ b/fullsizelayout.cpp @@ -41,7 +41,7 @@ bool FullSizeLayout::hasHeightForWidth() const } int FullSizeLayout::heightForWidth(int width) const { - int h = -1; + int h = 0; for (auto child : itemList) { h = std::max(h, child->heightForWidth(width)); @@ -58,7 +58,7 @@ QLayoutItem* FullSizeLayout::itemAt(int index) const } QSize FullSizeLayout::minimumSize() const { - QSize size; + QSize size(0, 0); for (auto child : itemList) { // auto wid = child->widget(); diff --git a/invalididexception.cpp b/invalididexception.cpp new file mode 100644 index 0000000..0f06d8c --- /dev/null +++ b/invalididexception.cpp @@ -0,0 +1,38 @@ +#include "invalididexception.hpp" + +InvalidIdException::InvalidIdException() +{ + +} + +InvalidRowIdException::InvalidRowIdException(TierRow::IdType id) + : _id(id), _what(std::format("id: {}", _id)) +{ + +} + +TierRow::IdType InvalidRowIdException::id() const +{ + return _id; +} + +const char* InvalidRowIdException::what() const noexcept +{ + return _what.c_str(); +} + +InvalidCardIdException::InvalidCardIdException(TierCard::IdType id) + : _id(id), _what(std::format("id: {}", _id)) +{ + +} + +TierCard::IdType InvalidCardIdException::id() const +{ + return _id; +} + +const char* InvalidCardIdException::what() const noexcept +{ + return _what.c_str(); +} diff --git a/invalididexception.hpp b/invalididexception.hpp new file mode 100644 index 0000000..58edd20 --- /dev/null +++ b/invalididexception.hpp @@ -0,0 +1,39 @@ +#ifndef INVALIDIDEXCEPTION_HPP +#define INVALIDIDEXCEPTION_HPP + +#include "tierrow.hpp" +#include "tiercard.hpp" + +#include + +class InvalidIdException : public QException +{ +public: + InvalidIdException(); +}; + +class InvalidRowIdException : public InvalidIdException +{ +public: + InvalidRowIdException(TierRow::IdType id); + TierRow::IdType id() const; + const char* what() const noexcept override; + +private: + const TierRow::IdType _id; + const std::string _what; +}; + +class InvalidCardIdException : public InvalidIdException +{ +public: + InvalidCardIdException(TierCard::IdType id); + TierCard::IdType id() const; + const char* what() const noexcept override; + +private: + const TierCard::IdType _id; + const std::string _what; +}; + +#endif // INVALIDIDEXCEPTION_HPP diff --git a/main.cpp b/main.cpp index 0ac1f55..a196b9e 100644 --- a/main.cpp +++ b/main.cpp @@ -4,6 +4,7 @@ #include "tiercardlayout.hpp" #include "utils.hpp" #include "rowholder.hpp" +#include "settings.hpp" #include #include @@ -56,8 +57,12 @@ int main(int argc, char *argv[]) { + auto settings = Settings::getSettable(); + settings->setCardSize(150, 150); + settings->setRowFade(.8f); QApplication a(argc, argv); MainWindow w; + settings->setMainWindow(&w); // QDockWidget dock; QScrollArea scroll; TierRow* row = TierRow::create(); @@ -67,11 +72,9 @@ int main(int argc, char *argv[]) row->setStyleSheet(makeBgColorString(QColor(35,35,35))); card1->setText("CARD1"); card1->setBgColor(QColor(64,255,64)); - card1->setFixedSize(150,150); TierCard* card2 = TierCard::create(); card2->setText("CARD2"); card2->setBgColor(QColor(64,64,255)); - card2->setFixedSize(150,150); row->addCard(card1); row->addCard(card2); @@ -81,13 +84,10 @@ int main(int argc, char *argv[]) TierCard* card3 = TierCard::create(); card3->setText("CARD1"); card3->setBgColor(QColor(64,12,164)); - card3->setFixedSize(150,150); TierCard* card4 = TierCard::create(); card4->setText("CARD2"); card4->setBgColor(QColor(164,64,155)); - card4->setFixedSize(150,150); auto card5 = TierCard::clone(card4); - card5->setFixedSize(150,150); row->addCard(card5); row2->addCard(card3); row2->addCard(card4); @@ -115,11 +115,12 @@ int main(int argc, char *argv[]) // a.setFont() // QTimer bg_changer; // bg_changer.setTimerType(Qt::PreciseTimer); - // bg_changer.setSingleShot(false); - // bg_changer.setInterval(1000); - // QObject::connect(&bg_changer, &QTimer::timeout, [&row]() + // bg_changer.setSingleShot(true); + // bg_changer.setInterval(2000); + // QObject::connect(&bg_changer, &QTimer::timeout, [&settings]() // { - // row.updateGeometry(); + // // row.updateGeometry(); + // settings->setCardSize(250,100); // }); // bg_changer.start(); // w.show(); diff --git a/settings.cpp b/settings.cpp index a83f6d4..01d42f5 100644 --- a/settings.cpp +++ b/settings.cpp @@ -1,5 +1,59 @@ #include "settings.hpp" +#include + Settings::Settings(QObject *parent) : QObject{parent} -{} +{ + +} + +const Settings* Settings::get() +{ + return getSettable(); +} + +Settings* Settings::getSettable() +{ + QMutexLocker locker(&lock); + if (!instance) + instance = new Settings(); + return instance; +} + +QSize Settings::cardSize() const +{ + return _cardSize; +} + +void Settings::setCardSize(QSize newSize) +{ + _cardSize = newSize; + emit cardSizeChange(newSize); +} + +void Settings::setCardSize(int w, int h) +{ + setCardSize(QSize(w, h)); +} + +void Settings::setMainWindow(QWidget* wid) +{ + _mainWindow = wid; +} + +QWidget* Settings::mainWindow() const +{ + return _mainWindow; +} + +void Settings::setRowFade(float fade) +{ + _rowFade = fade; + emit rowFadeChange(fade); +} + +float Settings::rowFade() const +{ + return _rowFade; +} diff --git a/settings.hpp b/settings.hpp index d68a30e..e277c01 100644 --- a/settings.hpp +++ b/settings.hpp @@ -2,14 +2,34 @@ #define SETTINGS_HPP #include +#include +#include class Settings : public QObject { Q_OBJECT public: + static const Settings* get(); + static Settings* getSettable(); + QSize cardSize() const; + void setCardSize(QSize size); + void setCardSize(int w, int h); + QWidget* mainWindow() const; + void setMainWindow(QWidget* wid); + float rowFade() const; + void setRowFade(float trans); + +private: explicit Settings(QObject *parent = nullptr); + inline static Settings* instance; + inline static QMutex lock; + QSize _cardSize; + QWidget* _mainWindow = nullptr; + float _rowFade = 0; signals: + void cardSizeChange(QSize newSize); + void rowFadeChange(float newFade); }; #endif // SETTINGS_HPP diff --git a/tiercard.cpp b/tiercard.cpp index a9b4b84..582f53f 100644 --- a/tiercard.cpp +++ b/tiercard.cpp @@ -2,40 +2,10 @@ #include "fullsizelayout.hpp" #include "qmimedata.h" #include "utils.hpp" +#include "settings.hpp" #include - -// TierCard::TierCard(QWidget *parent) -// : QWidget{parent} -// { -// auto bg_layout= new QVBoxLayout(this); -// bg_layout->setSpacing(0); -// bg_layout->setContentsMargins(0, 0, 0, 0); -// background = new QWidget(); -// bg_layout->addWidget(background); -// auto img_layout = new QVBoxLayout(background); -// img_layout->setSpacing(0); -// img_layout->setContentsMargins(0, 0, 0, 0); -// image = new AspectRatioPixmapLabel(); -// img_layout->addWidget(image, 0, Qt::AlignCenter); -// image->setAttribute(Qt::WA_TranslucentBackground); -// image->setAlignment(Qt::AlignCenter); -// auto txt_layout = new QVBoxLayout(image); -// txt_layout->setSpacing(0); -// txt_layout->setContentsMargins(0, 0, 0, 0); -// text_label = new QLabel(); -// id_label = new QLabel("4"); -// text_label->setWordWrap(true); -// // text_label->setTextFormat(Qt::RichText); -// text_label->setAlignment(Qt::AlignHCenter | Qt::AlignTop); -// id_label->setAlignment(Qt::AlignLeft | Qt::AlignBottom); -// text_label->setAttribute(Qt::WA_TranslucentBackground); -// id_label->setAttribute(Qt::WA_TranslucentBackground); -// txt_layout->addWidget(text_label, Qt::AlignTop | Qt::AlignHCenter); -// txt_layout->addWidget(id_label, Qt::AlignBottom, Qt::AlignLeft); -// text_label->setProperty("cssClass", "tierCardText"); -// id_label->setProperty("cssClass", "tierCardId"); -// } +#include TierCard::IdType TierCard::getAvailableId() { @@ -72,7 +42,8 @@ TierCard* TierCard::clone(TierCard *other, QWidget* parent) card = new TierCard(id, parent); card->setText(other->getText()); auto img = other->getImage(); - card->setImage(img); + if (!img.isNull()) + card->setImage(img); card->setBgColor(other->getBgColor()); idMap[id] = card; return card; @@ -81,6 +52,9 @@ TierCard* TierCard::clone(TierCard *other, QWidget* parent) TierCard::TierCard(IdType id, QWidget* parent) : QWidget{parent} { + auto settings = Settings::get(); + connect(settings, SIGNAL(cardSizeChange(QSize)), + this, SLOT(cardResize(QSize))); background = new QWidget(); image = new AspectRatioPixmapLabel(); image->setAttribute(Qt::WA_TranslucentBackground); @@ -102,6 +76,7 @@ TierCard::TierCard(IdType id, QWidget* parent) layout->addWidget(textLabel); layout->addWidget(idLabel); this->setLayout(layout); + cardResize(settings->cardSize()); } TierCard::~TierCard() @@ -158,9 +133,11 @@ void TierCard::mousePressEvent(QMouseEvent* event) render(&pix); QDrag drag(this); drag.setPixmap(pix); + drag.setHotSpot(QPoint(pix.size().width() / 2, + pix.size().height() / 2)); QByteArray itemData; QDataStream dataStream(&itemData, QIODevice::WriteOnly); - auto _id = getId(); + TierCard::IdType _id = getId(); dataStream.writeRawData(reinterpret_cast(&_id), sizeof(_id)); QMimeData *mimeData = new QMimeData; mimeData->setData(MimeType, itemData); @@ -183,3 +160,8 @@ TierCard* TierCard::getFromId(IdType id) return nullptr; return &(*iter->second); } + +void TierCard::cardResize(QSize newSize) +{ + setFixedSize(newSize); +} diff --git a/tiercard.hpp b/tiercard.hpp index 2139a3a..65a659d 100644 --- a/tiercard.hpp +++ b/tiercard.hpp @@ -43,6 +43,9 @@ private: static void releaseId(IdType id); inline static std::unordered_map idMap; +public slots: + void cardResize(QSize newSize); + signals: }; diff --git a/tierplaceholder.cpp b/tierplaceholder.cpp new file mode 100644 index 0000000..463ad0e --- /dev/null +++ b/tierplaceholder.cpp @@ -0,0 +1,24 @@ +#include "tierplaceholder.hpp" +#include "settings.hpp" + +TierPlaceholder::TierPlaceholder(QWidget *parent) + : QWidget{parent} +{ + auto settings = Settings::get(); + connect(settings, SIGNAL(cardSizeChange(QSize)), + this, SLOT(cardResize(QSize))); + QSizePolicy sp; + sp.setRetainSizeWhenHidden(true); + sp.setHorizontalPolicy(QSizePolicy::Fixed); + sp.setVerticalPolicy(QSizePolicy::Fixed); + setSizePolicy(sp); + //TODO potentially a race condition if card size is + //changed between settings->cardSize() being evaluated + //and cardResize() being called + cardResize(settings->cardSize()); +} + +void TierPlaceholder::cardResize(QSize newSize) +{ + setFixedSize(newSize); +} diff --git a/tierplaceholder.hpp b/tierplaceholder.hpp new file mode 100644 index 0000000..104c955 --- /dev/null +++ b/tierplaceholder.hpp @@ -0,0 +1,18 @@ +#ifndef TIERPLACEHOLDER_HPP +#define TIERPLACEHOLDER_HPP + +#include + +class TierPlaceholder : public QWidget +{ + Q_OBJECT +public: + explicit TierPlaceholder(QWidget *parent = nullptr); + +public slots: + void cardResize(QSize newSize); + +signals: +}; + +#endif // TIERPLACEHOLDER_HPP diff --git a/tierrow.cpp b/tierrow.cpp index 5a368a9..959f36a 100644 --- a/tierrow.cpp +++ b/tierrow.cpp @@ -1,49 +1,44 @@ #include "tierrow.hpp" -#include "fullsizelayout.hpp" +// #include "fullsizelayout.hpp" #include "qmimedata.h" #include "utils.hpp" +#include "settings.hpp" +#include "invalididexception.hpp" #include TierRow::TierRow(IdType id, QWidget *parent) - : QWidget{parent} + : QWidget{parent}, _id(id) { + auto settings = Settings::get(); setAcceptDrops(true); - auto fadeLayout = new FullSizeLayout(); - fadeLayout->setSizeConstraint(QLayout::SetMinimumSize); - fadeLayout->setContentsMargins(0, 0, 0, 0); - fadeLayout->setSpacing(0); - bgFadeContainer = new QWidget(); - bgFadeContainer->setStyleSheet(makeBgColorString(QColor(255,255,255,100))); - bgFadeContainer->setMinimumSize(0, 0); - fadeLayout->addWidget(bgFadeContainer); - this->setLayout(fadeLayout); auto splitLayout = new QHBoxLayout(); splitLayout->setSizeConstraint(QLayout::SetMinimumSize); splitLayout->setContentsMargins(0, 0, 0, 0); splitLayout->setSpacing(0); + this->setLayout(splitLayout); titleCard = new TierRowTitleCard(id); cardContainer = new QWidget(); - cardContainer->setMinimumSize(0, 0); - titleCard->setFixedSize(150, 150); cardContainer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); splitLayout->addWidget(titleCard, 0, Qt::AlignLeft | Qt::AlignTop); splitLayout->addWidget(cardContainer, 1, Qt::AlignTop); - cardContainer->setStyleSheet(makeBgColorString(QColor(60,60,60,100))); - - bgFadeContainer->setLayout(splitLayout); - cardLayout = new FlowLayout(); + // cardContainer->setStyleSheet(makeBgColorString(QColor(60,60,60,100))); + cardLayout = new FlowLayout(nullptr, 0, 0, 0); cardLayout->setSizeConstraint(QLayout::SetMinimumSize); cardLayout->setContentsMargins(0, 0, 0, 0); cardLayout->setSpacing(0); cardContainer->setLayout(cardLayout); - // setMinimumSize(0, 0); + connect(settings, SIGNAL(cardSizeChange(QSize)), + this, SLOT(cardResize(QSize))); + connect(settings, SIGNAL(rowFadeChange(float)), + this, SLOT(fadeChange(float))); + cardResize(settings->cardSize()); } TierRow::~TierRow() { - qDebug() << "Row destructor called " << id(); + // qDebug() << "Row destructor called " << id(); if (idMap.erase(id()) == 0) { qDebug() << "Row id not found during destructor"; @@ -60,12 +55,20 @@ void TierRow::addCard(TierCard* card) // } void TierRow::setColor(QColor color) { + auto settings = Settings::get(); + auto fade = settings->rowFade(); + if (fade < 0) + fade = 0; + if (fade > 1) + fade = 1; _color = color; + const QColor wh(255,255,255); titleCard->setColor(color); - auto str = makeBgColorString(color); + auto fadedColor = blend(wh, color, fade); + auto str = makeBgColorString(fadedColor); setStyleSheet(str); } -QColor TierRow::color() +QColor TierRow::color() const { return _color; } @@ -73,13 +76,13 @@ void TierRow::setText(QString text) { titleCard->setText(text); } -QString TierRow::text() +QString TierRow::text() const { return titleCard->text(); } -uint32_t TierRow::id() +uint32_t TierRow::id() const { - return titleCard->id(); + return _id; } void TierRow::resizeEvent(QResizeEvent* event) @@ -90,7 +93,8 @@ void TierRow::resizeEvent(QResizeEvent* event) void TierRow::recalcMaxHeight() { - setMaximumHeight(cardLayout->totalMinimumHeightForWidth(cardContainer->width())); + setMaximumHeight(cardLayout->totalMinimumHeightForWidth( + cardContainer->width())); } TierRow* TierRow::create(QWidget* parent) @@ -117,21 +121,25 @@ TierRow* TierRow::getFromId(IdType id) return iter->second; } -void TierRow::dropEvent(QDropEvent *event) +void TierRow::dropEvent(QDropEvent* event) { qDebug() << "drop event"; + // auto source = event->source(); //card dragged to this row if (event->mimeData()->hasFormat(TierCard::MimeType)) { if (event->proposedAction() == Qt::MoveAction) { + clearPlaceholder(); auto data = event->mimeData()->data(TierCard::MimeType); QDataStream stream(&data, QIODevice::ReadOnly); TierCard::IdType cardId; - uint dataLen = 0; - stream.readRawData(reinterpret_cast(&cardId), dataLen); + stream.readRawData(reinterpret_cast(&cardId), sizeof(cardId)); auto card = TierCard::getFromId(cardId); - addCard(card); + if (!card) + throw InvalidCardIdException(cardId); + // addCard(card); + cardLayout->setIndex(card, calculateIndex(event->position())); event->acceptProposedAction(); } else @@ -146,13 +154,24 @@ void TierRow::dropEvent(QDropEvent *event) } } -void TierRow::dragEnterEvent(QDragEnterEvent *event) +void TierRow::dragEnterEvent(QDragEnterEvent* event) { qDebug() << "drag enter event"; + if (cardCount() == 0) + { + qDebug() << "enter empty"; + } if (event->mimeData()->hasFormat(TierCard::MimeType)) { if (event->proposedAction() == Qt::MoveAction) { + if (placeholder != nullptr) + { + qDebug() << "placeholder not previously cleared"; + clearPlaceholder(); + } + placeholder = new TierPlaceholder(); + cardLayout->setIndex(placeholder, calculateIndex(event->position())); event->setDropAction(Qt::MoveAction); event->accept(cardContainer->rect()); } @@ -172,13 +191,18 @@ void TierRow::dragEnterEvent(QDragEnterEvent *event) } } -void TierRow::dragMoveEvent(QDragMoveEvent *event) +void TierRow::dragMoveEvent(QDragMoveEvent* event) { - qDebug() << "drag move event"; if (event->mimeData()->hasFormat(TierCard::MimeType)) { if (event->proposedAction() == Qt::MoveAction) { + if (cardCount() == 1) + { + qDebug() << "moving in empty"; + } + if (placeholder) + cardLayout->setIndex(placeholder, calculateIndex(event->position())); event->acceptProposedAction(); } else @@ -190,9 +214,62 @@ void TierRow::dragMoveEvent(QDragMoveEvent *event) else if (event->mimeData()->hasImage()) { event->setDropAction(Qt::CopyAction); - event->accept(); + //TODO event->accept(); + event->ignore(); } - else { + else + { event->ignore(); } } + +void TierRow::dragLeaveEvent(QDragLeaveEvent* event) +{ + clearPlaceholder(); +} + +void TierRow::clearPlaceholder() +{ + placeholder->setParent(nullptr); + placeholder->deleteLater(); + placeholder = nullptr; +} + +int TierRow::calculateIndex(QPointF loc) +{ + auto settings = Settings::get(); + auto cardSize = settings->cardSize(); + auto cardCenter = loc + QPointF(cardSize.width() / 2, + cardSize.height() / 2); + auto lastBefore = 0; + for (int i = 0; i < cardLayout->count(); i++) + { + auto refCard = cardLayout->itemAt(i); + auto refPos = refCard->widget()->pos(); + if (cardCenter.y() > refPos.y() + && cardCenter.x() > refPos.x()) + lastBefore = i; + else + break; + } + return lastBefore; +} + +void TierRow::cardResize(QSize newSize) +{ + titleCard->setFixedSize(newSize); + cardContainer->setMinimumHeight(newSize.height()); + cardContainer->updateGeometry(); + updateGeometry(); + Settings::get()->mainWindow()->updateGeometry(); +} + +void TierRow::fadeChange(float newFade) +{ + setColor(_color); +} + +int TierRow::cardCount() const +{ + return cardLayout->count(); +} diff --git a/tierrow.hpp b/tierrow.hpp index c745a29..ee499fb 100644 --- a/tierrow.hpp +++ b/tierrow.hpp @@ -4,10 +4,13 @@ #include "tiercard.hpp" #include "tierrowtitlecard.hpp" #include "flowlayout.h" +#include "tierplaceholder.hpp" #include #include +#include + class TierRow : public QWidget { Q_OBJECT @@ -18,31 +21,37 @@ public: ~TierRow(); void addCard(TierCard* card); TierCard* takeCard(uint32_t id); + int cardCount() const; void setColor(QColor color); - QColor color(); + QColor color() const; void setText(QString text); - QString text(); - uint32_t id(); + QString text() const; + uint32_t id() const; protected: void resizeEvent(QResizeEvent* event) override; // void mousePressEvent(QMouseEvent* event) override; void dropEvent(QDropEvent *event) override; - void dragEnterEvent(QDragEnterEvent *event) override; - void dragMoveEvent(QDragMoveEvent *event) override; + void dragEnterEvent(QDragEnterEvent* event) override; + void dragLeaveEvent(QDragLeaveEvent* event) override; + void dragMoveEvent(QDragMoveEvent* event) override; private: explicit TierRow(IdType id, QWidget* parent = nullptr); TierRowTitleCard* titleCard; - QWidget* bgFadeContainer; QWidget* cardContainer; - QLayout* cardLayout; + FlowLayout* cardLayout; QColor _color; + const IdType _id; + TierPlaceholder* placeholder = nullptr; void recalcMaxHeight(); + void clearPlaceholder(); + int calculateIndex(QPointF loc); inline static std::unordered_map idMap; public slots: - void cardResize(QRect newSize); + void cardResize(QSize newSize); + void fadeChange(float newFade); signals: diff --git a/tierrowtitlecard.cpp b/tierrowtitlecard.cpp index f1e95f7..ae4fee8 100644 --- a/tierrowtitlecard.cpp +++ b/tierrowtitlecard.cpp @@ -5,7 +5,7 @@ #include TierRowTitleCard::TierRowTitleCard(IdType id, QWidget *parent) - : QFrame{parent} + : QFrame{parent}, _id(id) { this->setFrameStyle(QFrame::Box | QFrame::Plain); auto layout = new FullSizeLayout(); @@ -38,5 +38,5 @@ void TierRowTitleCard::setColor(QColor color) uint32_t TierRowTitleCard::id() const { - return static_cast(idLabel->text().toUInt()); + return _id; } diff --git a/tierrowtitlecard.hpp b/tierrowtitlecard.hpp index c6ca041..1ea740b 100644 --- a/tierrowtitlecard.hpp +++ b/tierrowtitlecard.hpp @@ -20,6 +20,7 @@ private: QColor _color; QLabel* titleLabel; QLabel* idLabel; + const IdType _id; signals: }; diff --git a/utils.hpp b/utils.hpp index 88f4ad7..174023c 100644 --- a/utils.hpp +++ b/utils.hpp @@ -1,7 +1,7 @@ #ifndef UTILS_HPP #define UTILS_HPP -#include "tierrow.hpp" +// #include "tierrow.hpp" #include #include @@ -53,4 +53,15 @@ T* findParentOfType(QObject* obj) } } +inline QColor blend(QColor first, QColor second, float portionFirst) +{ + const float& pf = portionFirst; + const float ps = 1 - pf; + return QColor( + first.red()*pf + second.red()*ps, + first.green()*pf + second.green()*ps, + first.blue()*pf + second.blue()*ps + ); +} + #endif // UTILS_HPP