-
Notifications
You must be signed in to change notification settings - Fork 1
/
CCardLabelNexus.cpp
200 lines (184 loc) · 6.5 KB
/
CCardLabelNexus.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "CCardLabelNexus.h"
#include "CCardLabel.h"
#include "model/CCardTable.h"
#include "CGlobalConfig.h"
#include <QApplication>
#include <QHelpEvent>
#include <QDesktopWidget>
#include <QBoxLayout>
#include <QDialog>
CCardLabelNexus* CCardLabelNexus::CARD_LABEL_NEXUS = 0;
CCardLabelNexus::CCardLabelNexus()
: QObject()
, mCardLabelToolTip(new QDialog())
, mCardLabelToolTipText(new QLabel())
, mCardLabelToolTipLayout(0)
, mCardLabelToolTipSummonLabel(0)
{
mCardLabelToolTip->setWindowFlags(Qt::ToolTip);
mCardLabelToolTip->setStyleSheet("QDialog{background-color: rgb(255, 255, 219);border-color: rgb(0, 0, 0);border-width: 1px;border-style: solid}");
mCardLabelToolTipLayout = new QBoxLayout(QBoxLayout::TopToBottom, mCardLabelToolTip);
mCardLabelToolTipLayout->setSpacing(5);
mCardLabelToolTipLayout->setMargin(5);
mCardLabelToolTipText->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
mCardLabelToolTipLayout->addWidget(mCardLabelToolTipText);
}
CCardLabelNexus::~CCardLabelNexus()
{
delete mCardLabelToolTip;
mCardLabelToolTip = 0;
}
CCardLabelNexus& CCardLabelNexus::getCardLabelNexus()
{
if (!CARD_LABEL_NEXUS)
{
CARD_LABEL_NEXUS = new CCardLabelNexus();
}
return *CARD_LABEL_NEXUS;
}
void CCardLabelNexus::updateCardPictures(const CCard & card)
{
emit pictureDownloaded(card);
}
void CCardLabelNexus::registerCardLabel(CCardLabel* label)
{
if (label)
{
connect(
label, SIGNAL(unitRightClicked(bool)),
this, SLOT(setCardLabelBlackListStatus(bool)));
connect(
label, SIGNAL(unitCtrlRightClicked(bool)),
this, SLOT(setCardLabelWhiteListStatus(bool)));
connect(
this, SIGNAL(pictureDownloaded(const CCard&)),
label, SLOT(updateCardLabelPicture(const CCard&)));
label->installEventFilter(this);
}
}
void CCardLabelNexus::unregisterCardLabel(CCardLabel *label)
{
if (label)
{
disconnect(
label, SIGNAL(unitRightClicked(bool)),
this, SLOT(setCardLabelBlackListStatus(bool)));
disconnect(
label, SIGNAL(unitCtrlRightClicked(bool)),
this, SLOT(setCardLabelWhiteListStatus(bool)));
label->removeEventFilter(this);
}
}
void CCardLabelNexus::setCardLabelBlackListStatus(bool isBlack)
{
CCardLabel* sendLabel = dynamic_cast<CCardLabel*>(QObject::sender());
if (sendLabel)
{
emit blackListStatusToggled(sendLabel->getCard(), isBlack);
}
}
void CCardLabelNexus::setCardLabelWhiteListStatus(bool isWhite)
{
CCardLabel* sendLabel = dynamic_cast<CCardLabel*>(QObject::sender());
if (sendLabel)
{
emit whiteListStatusToggled(sendLabel->getCard(), isWhite);
}
}
void CCardLabelNexus::showCardLabelToolTip(const CCard &card, int x, int y)
{
if (!mCardLabelToolTip->isVisible() && card.isValid())
{
const CGlobalConfig &cfg = CGlobalConfig::getCfg();
const CCardTable &cardTable = CCardTable::getCardTable();
mCardLabelToolTipSummonLabel->setVisible(false);
const QList<CCardSkill> &skills = card.getSkills();
QStringList skillDescr;
skillDescr.append(QString("<b>%1</b><br>").arg(card.getName()));
QString subTitleStr = card.isUnique() ? "Unique " : "";
switch(card.getFaction())
{
case EImperialFaction: subTitleStr += "Imperial"; break;
case EBloodthirstyFaction: subTitleStr += "Bloodthirsty"; break;
case EXenoFaction: subTitleStr += "Xeno"; break;
case ERighteousFaction: subTitleStr += "Righteous"; break;
case ERaiderFaction: subTitleStr += "Raider"; break;
default: break;
}
skillDescr.append(QString("<i>%1</i><table valign='middle'>").arg(subTitleStr));
for (int i = 0; i < skills.size(); ++i)
{
const CSkill& curSkill = cardTable.getSkillForId(skills.at(i).getId());
if (curSkill.isValid())
{
skillDescr.append(QString("<tr><td><img src='%1.png'/></td><td> %2</td></tr>")
.arg(cfg.getResourcePicturePath() + curSkill.getPicture())
.arg(curSkill.makeSignature(skills.at(i), true)));
if (curSkill.isSummon())
{
const CCard &summonCard = cardTable.getCardForId(skills.at(i).getX());
mCardLabelToolTipSummonLabel->setCard(summonCard);
mCardLabelToolTipSummonLabel->setVisible(true);
}
}
}
skillDescr.append(QString("</table>"));
mCardLabelToolTipText->setText(skillDescr.join(""));
mCardLabelToolTipLayout->activate();
mCardLabelToolTip->adjustSize();
int reqW = mCardLabelToolTip->width() + 5;
int reqH = mCardLabelToolTip->height() + 20;
int xRel = (QApplication::desktop()->width() - x < reqW)
? -reqW
: 5;
int yRel = (QApplication::desktop()->height() - y < reqH)
? -reqH
: 20;
mCardLabelToolTip->setGeometry(x + xRel, y + yRel, 80, 40);
mCardLabelToolTip->setVisible(true);
}
}
void CCardLabelNexus::hideCardLabelToolTip()
{
mCardLabelToolTip->setVisible(false);
}
bool CCardLabelNexus::eventFilter(QObject *obj, QEvent *e)
{
switch (e->type())
{
case QEvent::ToolTip:
{
QHelpEvent *he = static_cast<QHelpEvent *>(e);
CCardLabel *cardLabel = dynamic_cast<CCardLabel*>(obj);
if (e && cardLabel)
{
addCardLabelToolTipSummonLabels();
showCardLabelToolTip(cardLabel->getCard(), he->globalX(), he->globalY());
he->accept();
}
return true;
}
//case QEvent::FocusOut:
case QEvent::Leave:
hideCardLabelToolTip();
return QObject::eventFilter(obj, e);
default:
{
// standard event processing
return QObject::eventFilter(obj, e);
}
}
}
void CCardLabelNexus::addCardLabelToolTipSummonLabels()
{
if (mCardLabelToolTipSummonLabel == 0 && mCardLabelToolTipSummonLabel == 0)
{
QBoxLayout* hLayout = new QBoxLayout(QBoxLayout::LeftToRight);
mCardLabelToolTipSummonLabel = new CCardLabel(0, true);
mCardLabelToolTipSummonLabel->setMaximumSize(80, 110);
hLayout->addWidget(mCardLabelToolTipSummonLabel);
hLayout->setMargin(0);
hLayout->addStretch();
mCardLabelToolTipLayout->addLayout(hLayout);
}
}