bunch of stuff. very basic drag/drop now working
This commit is contained in:
185
tiercard.cpp
Normal file
185
tiercard.cpp
Normal file
@@ -0,0 +1,185 @@
|
||||
#include "tiercard.hpp"
|
||||
#include "fullsizelayout.hpp"
|
||||
#include "qmimedata.h"
|
||||
#include "utils.hpp"
|
||||
|
||||
#include <QVBoxLayout>
|
||||
|
||||
// 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");
|
||||
// }
|
||||
|
||||
TierCard::IdType TierCard::getAvailableId()
|
||||
{
|
||||
for (IdType id = 1;; id++)
|
||||
{
|
||||
if (idMap.find(id) == idMap.end())
|
||||
{
|
||||
return id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TierCard::releaseId(IdType id)
|
||||
{
|
||||
if (idMap.erase(id) == 0)
|
||||
{
|
||||
qDebug() << "Card id not found during destructor";
|
||||
}
|
||||
}
|
||||
|
||||
TierCard* TierCard::create(QWidget* parent)
|
||||
{
|
||||
TierCard* card;
|
||||
auto id = getAvailableId();
|
||||
card = new TierCard(id, parent);
|
||||
idMap[id] = card;
|
||||
return card;
|
||||
}
|
||||
|
||||
TierCard* TierCard::clone(TierCard *other, QWidget* parent)
|
||||
{
|
||||
TierCard* card;
|
||||
auto id = getAvailableId();
|
||||
card = new TierCard(id, parent);
|
||||
card->setText(other->getText());
|
||||
auto img = other->getImage();
|
||||
card->setImage(img);
|
||||
card->setBgColor(other->getBgColor());
|
||||
idMap[id] = card;
|
||||
return card;
|
||||
}
|
||||
|
||||
TierCard::TierCard(IdType id, QWidget* parent)
|
||||
: QWidget{parent}
|
||||
{
|
||||
background = new QWidget();
|
||||
image = new AspectRatioPixmapLabel();
|
||||
image->setAttribute(Qt::WA_TranslucentBackground);
|
||||
image->setAlignment(Qt::AlignCenter);
|
||||
textLabel = new QLabel();
|
||||
idLabel = new QLabel();
|
||||
textLabel->setWordWrap(true);
|
||||
// text_label->setTextFormat(Qt::RichText);
|
||||
textLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
|
||||
idLabel->setAlignment(Qt::AlignLeft | Qt::AlignBottom);
|
||||
textLabel->setAttribute(Qt::WA_TranslucentBackground);
|
||||
idLabel->setAttribute(Qt::WA_TranslucentBackground);
|
||||
textLabel->setObjectName("tierCardText");
|
||||
idLabel->setObjectName("tierCardId");
|
||||
idLabel->setText(QString::number(id));
|
||||
auto layout = new FullSizeLayout();
|
||||
layout->addWidget(background);
|
||||
layout->addWidget(image);
|
||||
layout->addWidget(textLabel);
|
||||
layout->addWidget(idLabel);
|
||||
this->setLayout(layout);
|
||||
}
|
||||
|
||||
TierCard::~TierCard()
|
||||
{
|
||||
releaseId(getId());
|
||||
// delete text_label;
|
||||
// text_label = nullptr;
|
||||
// delete id_label;
|
||||
// id_label = nullptr;
|
||||
}
|
||||
|
||||
void TierCard::setText(QString str)
|
||||
{
|
||||
textLabel->setText(str);
|
||||
}
|
||||
|
||||
QString TierCard::getText() const
|
||||
{
|
||||
return textLabel->text();
|
||||
}
|
||||
|
||||
uint32_t TierCard::getId() const
|
||||
{
|
||||
return static_cast<uint32_t>(idLabel->text().toUInt());
|
||||
}
|
||||
|
||||
void TierCard::setBgColor(QColor color)
|
||||
{
|
||||
bgColor = color;
|
||||
auto str = makeBgColorString(color);
|
||||
background->setStyleSheet(str);
|
||||
}
|
||||
|
||||
QColor TierCard::getBgColor()
|
||||
{
|
||||
return bgColor;
|
||||
}
|
||||
|
||||
void TierCard::setImage(const QPixmap img)
|
||||
{
|
||||
// QMetaObject::invokeMethod()
|
||||
image->setPixmap(img);
|
||||
}
|
||||
|
||||
QPixmap TierCard::getImage() const
|
||||
{
|
||||
return image->pixmap();
|
||||
}
|
||||
|
||||
void TierCard::mousePressEvent(QMouseEvent* event)
|
||||
{
|
||||
qDebug() << "mouse press event";
|
||||
QPixmap pix(size());
|
||||
render(&pix);
|
||||
QDrag drag(this);
|
||||
drag.setPixmap(pix);
|
||||
QByteArray itemData;
|
||||
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
|
||||
auto _id = getId();
|
||||
dataStream.writeRawData(reinterpret_cast<char*>(&_id), sizeof(_id));
|
||||
QMimeData *mimeData = new QMimeData;
|
||||
mimeData->setData(MimeType, itemData);
|
||||
drag.setMimeData(mimeData);
|
||||
hide();
|
||||
if (drag.exec() == Qt::MoveAction)
|
||||
{
|
||||
show();
|
||||
}
|
||||
else
|
||||
{
|
||||
show();
|
||||
}
|
||||
}
|
||||
|
||||
TierCard* TierCard::getFromId(IdType id)
|
||||
{
|
||||
auto iter = idMap.find(id);
|
||||
if (iter == idMap.end())
|
||||
return nullptr;
|
||||
return &(*iter->second);
|
||||
}
|
||||
Reference in New Issue
Block a user