Warning, file /games/libkdegames/tests/kgamepopupitemtest.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2007 Dmitry Suzdalev <dimsuz@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #include "kgamepopupitemtest.h"
0008 
0009 // KF
0010 #include <KAboutData>
0011 #include <KActionCollection>
0012 #include <KColorScheme>
0013 #include <KFileDialog>
0014 #include <KLocalizedString>
0015 // Qt
0016 #include <QApplication>
0017 #include <QCommandLineParser>
0018 #include <QGraphicsScene>
0019 #include <QTimer>
0020 
0021 KGpiMainWindow::KGpiMainWindow()
0022     : KXmlGuiWindow()
0023     , m_replaceMode(KGamePopupItem::LeavePrevious)
0024 {
0025     QWidget *wid = new QWidget(this);
0026     m_mainWid.setupUi(wid);
0027     m_popupItem = new KGamePopupItem;
0028     connect(m_popupItem, &KGamePopupItem::linkActivated, this, &KGpiMainWindow::onLinkClicked);
0029     m_textItem = new QGraphicsSimpleTextItem;
0030 
0031     KStandardAction::quit(this, &KGpiMainWindow::close, actionCollection());
0032 
0033     m_scene = new QGraphicsScene;
0034     m_scene->setSceneRect(-1000, -1000, 2000, 2000);
0035     QLinearGradient gradient(QPointF(-1000, -1000), QPointF(2000, 2000));
0036     gradient.setColorAt(0, Qt::white);
0037     gradient.setColorAt(1, Qt::blue);
0038 
0039     m_scene->setBackgroundBrush(gradient);
0040 
0041     m_scene->addItem(m_popupItem);
0042     m_scene->addItem(m_textItem);
0043 
0044     m_mainWid.graphicsView->setScene(m_scene);
0045 
0046     KColorScheme kcs(QPalette::Active, KColorScheme::Tooltip);
0047     m_mainWid.textColor->setColor(kcs.foreground(KColorScheme::NormalText).color());
0048     m_mainWid.bkgndColor->setColor(kcs.background().color());
0049 
0050     connect(m_mainWid.popupTL, &QPushButton::clicked, this, &KGpiMainWindow::onPopupTL);
0051     connect(m_mainWid.popupTR, &QPushButton::clicked, this, &KGpiMainWindow::onPopupTR);
0052     connect(m_mainWid.popupBL, &QPushButton::clicked, this, &KGpiMainWindow::onPopupBL);
0053     connect(m_mainWid.popupBR, &QPushButton::clicked, this, &KGpiMainWindow::onPopupBR);
0054     connect(m_mainWid.popupCenter, &QPushButton::clicked, this, &KGpiMainWindow::onPopupCenter);
0055     connect(m_mainWid.forceInstantHide, &QPushButton::clicked, this, &KGpiMainWindow::doInstantHide);
0056     connect(m_mainWid.forceAnimatedHide, &QPushButton::clicked, this, &KGpiMainWindow::doAnimatedHide);
0057     connect(m_mainWid.changeIcon, &QPushButton::clicked, this, &KGpiMainWindow::changeIcon);
0058     connect(m_mainWid.opacity, &QSlider::valueChanged, this, &KGpiMainWindow::changeOpacity);
0059     connect(m_mainWid.textColor, &KColorButton::changed, this, &KGpiMainWindow::textColorChanged);
0060     connect(m_mainWid.bkgndColor, &KColorButton::changed, this, &KGpiMainWindow::bkgndColorChanged);
0061     connect(m_mainWid.leavePrevious, &QRadioButton::clicked, this, &KGpiMainWindow::replaceModeChanged);
0062     connect(m_mainWid.replacePrevious, &QRadioButton::clicked, this, &KGpiMainWindow::replaceModeChanged);
0063     connect(m_mainWid.cornersType, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), this, &KGpiMainWindow::sharpnessChanged);
0064 
0065     connect(m_mainWid.mesTimeout, static_cast<void (KIntSpinBox::*)(int)>(&KIntSpinBox::valueChanged), this, &KGpiMainWindow::onTimeoutChanged);
0066     m_mainWid.mesTimeout->setValue(m_popupItem->messageTimeout());
0067     m_mainWid.mesTimeout->setSuffix(ki18np(" millisecond", " milliseconds"));
0068     m_mainWid.opacity->setValue(static_cast<int>(m_popupItem->messageOpacity() * 100));
0069     setCentralWidget(wid);
0070 
0071     setupGUI();
0072 }
0073 
0074 int main(int argc, char **argv)
0075 {
0076     KAboutData aboutData(QLatin1String("kgamepopupitemtest"), i18n("kgamepopupitemtest"), QLatin1String("0.1"));
0077     QApplication app(argc, argv);
0078     QCommandLineParser parser;
0079     KAboutData::setApplicationData(aboutData);
0080     parser.addVersionOption();
0081     parser.addHelpOption();
0082     aboutData.setupCommandLine(&parser);
0083     parser.process(app);
0084     aboutData.processCommandLine(&parser);
0085     KGpiMainWindow *win = new KGpiMainWindow;
0086     win->show();
0087     return a.exec();
0088 }
0089 
0090 void KGpiMainWindow::onPopupTL()
0091 {
0092     QString str = !m_mainWid.showRichText->isChecked()
0093         ? "Heya! Popping up!"
0094         : "<font color=\"red\">Heya!</font> Click <a href=\"oh-oh-i-am-the-Link\">the link</a><br> and <b>a</b> message should appear in the scene";
0095     m_popupItem->showMessage(str, KGamePopupItem::TopLeft, m_replaceMode);
0096 }
0097 
0098 void KGpiMainWindow::onPopupTR()
0099 {
0100     QString str = !m_mainWid.showRichText->isChecked()
0101         ? "Yippie! Popping up!"
0102         : "<font color=\"green\">Yippie!</font> Click <a href=\"oh-oh-i-am-the-Link\">the link</a><br> and <b>a</b> message should appear in the scene";
0103     m_popupItem->showMessage(str, KGamePopupItem::TopRight, m_replaceMode);
0104 }
0105 
0106 void KGpiMainWindow::onPopupBL()
0107 {
0108     QString str = !m_mainWid.showRichText->isChecked() ? "Horray! Popping up!"
0109                                                        : "<font color=\"yellow\">Horray</font><i>,comrades</i>! Click <a href=\"oh-oh-i-am-the-Link\">the "
0110                                                          "link</a><br> and <b>a</b> message should appear in the scene";
0111     m_popupItem->showMessage(str, KGamePopupItem::BottomLeft, m_replaceMode);
0112 }
0113 
0114 void KGpiMainWindow::onPopupBR()
0115 {
0116     QString str = !m_mainWid.showRichText->isChecked() ? "Popping up! I like it, yeah!"
0117                                                        : "<font color=\"blue\">Wow. Just blue WOW</font>! Click <a href=\"oh-oh-i-am-the-Link\">the "
0118                                                          "link</a><br> and <b>a</b> message should appear in the scene";
0119     m_popupItem->showMessage(str, KGamePopupItem::BottomRight, m_replaceMode);
0120 }
0121 
0122 void KGpiMainWindow::onLinkClicked(const QString &link)
0123 {
0124     QRectF visibleRect = m_mainWid.graphicsView->mapToScene(m_mainWid.graphicsView->contentsRect()).boundingRect();
0125     m_textItem->setText("Hi! I'm the message that should appear :-). You clicked link: " + link);
0126     m_textItem->setPos(visibleRect.left() + visibleRect.width() / 2 - m_textItem->boundingRect().width() / 2, visibleRect.top() + visibleRect.height() / 2);
0127     m_textItem->show();
0128     QTimer::singleShot(2000, this, &KGpiMainWindow::hideTextItem);
0129 }
0130 
0131 void KGpiMainWindow::hideTextItem()
0132 {
0133     m_textItem->hide();
0134 }
0135 
0136 void KGpiMainWindow::onTimeoutChanged(int msec)
0137 {
0138     m_popupItem->setMessageTimeout(msec);
0139 }
0140 
0141 void KGpiMainWindow::changeIcon()
0142 {
0143     QString newPix = KFileDialog::getImageOpenUrl(KUrl(), this).path();
0144     if (newPix.isEmpty())
0145         return;
0146     QPixmap pix(newPix);
0147     m_popupItem->setMessageIcon(newPix);
0148 }
0149 
0150 void KGpiMainWindow::doInstantHide()
0151 {
0152     m_popupItem->forceHide(KGamePopupItem::InstantHide);
0153 }
0154 
0155 void KGpiMainWindow::doAnimatedHide()
0156 {
0157     m_popupItem->forceHide(KGamePopupItem::AnimatedHide);
0158 }
0159 
0160 void KGpiMainWindow::changeOpacity(int opa)
0161 {
0162     m_popupItem->setMessageOpacity(opa / 100.0);
0163 }
0164 
0165 void KGpiMainWindow::textColorChanged(const QColor &col)
0166 {
0167     m_popupItem->setTextColor(col);
0168 }
0169 
0170 void KGpiMainWindow::bkgndColorChanged(const QColor &col)
0171 {
0172     m_popupItem->setBackgroundBrush(col);
0173 }
0174 
0175 void KGpiMainWindow::replaceModeChanged()
0176 {
0177     if (m_mainWid.leavePrevious->isChecked())
0178         m_replaceMode = KGamePopupItem::LeavePrevious;
0179     else
0180         m_replaceMode = KGamePopupItem::ReplacePrevious;
0181 }
0182 
0183 void KGpiMainWindow::sharpnessChanged(int idx)
0184 {
0185     if (idx == 0)
0186         m_popupItem->setSharpness(KGamePopupItem::Square);
0187     else if (idx == 1)
0188         m_popupItem->setSharpness(KGamePopupItem::Sharp);
0189     else if (idx == 2)
0190         m_popupItem->setSharpness(KGamePopupItem::Soft);
0191     else if (idx == 3)
0192         m_popupItem->setSharpness(KGamePopupItem::Softest);
0193 }
0194 
0195 void KGpiMainWindow::onPopupCenter()
0196 {
0197     QString str = !m_mainWid.showRichText->isChecked()
0198         ? "Popping, popping, popping up!"
0199         : "<font color=\"red\">Heya!</font> Click <a href=\"oh-oh-i-am-the-Link\">the link</a><br> and <b>a</b> message should appear in the scene";
0200     m_popupItem->showMessage(str, KGamePopupItem::Center, m_replaceMode);
0201 }
0202 
0203 #include "moc_kgamepopupitemtest.cpp"