File indexing completed on 2024-04-21 03:51:00

0001 /*
0002     SPDX-FileCopyrightText: 2014 Andreas Xavier
0003     SPDX-FileCopyrightText: 2014 Inge Wallin <inge@lysator.liu.se>
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 // Own
0008 #include "collectionwidget.h"
0009 
0010 // Parley
0011 
0012 // KDE
0013 #include <KLocalizedString>
0014 
0015 // Qt
0016 #include <QColor>
0017 #include <QGraphicsDropShadowEffect>
0018 #include <QLabel>
0019 #include <QPainter>
0020 #include <QPen>
0021 #include <QPushButton>
0022 #include <QVBoxLayout>
0023 
0024 // Parley
0025 #include "utils.h"
0026 
0027 // Size constants for the collection widgets
0028 int COLLWIDTH = 155; // Width in pixels of a collection widget
0029 // int COLLHEIGHT1 = 250; // Height in pixels of a collection widget not yet fully learned
0030 int COLLHEIGHT1 = 175; // Height in pixels of a collection widget not yet fully learned
0031 int COLLHEIGHT2 = 125; // Height in pixels of a collection widget fully learned
0032 
0033 // ================================================================
0034 //                        private classes
0035 
0036 // The RemoveButton is a button that the user can press in a collection to
0037 // remove the collection from the word bank.
0038 
0039 class RemoveButton : public QPushButton
0040 {
0041 public:
0042     RemoveButton(QWidget *parent = nullptr);
0043 
0044 protected:
0045     void paintEvent(QPaintEvent *) override;
0046 };
0047 
0048 RemoveButton::RemoveButton(QWidget *parent)
0049     : QPushButton(parent)
0050 {
0051 }
0052 
0053 void RemoveButton::paintEvent(QPaintEvent *)
0054 {
0055     QPainter painter(this);
0056     QPen pen(QColor(255, 255, 255));
0057     painter.setPen(pen);
0058     painter.setRenderHint(QPainter::Antialiasing, true);
0059     QBrush brush(QColor(49, 54, 59));
0060     painter.setBrush(brush);
0061 
0062     painter.drawEllipse(QRectF(1.0, 1.0, height() - 1.0, height() - 1.0));
0063     painter.setFont(QFont(QStringLiteral("Helvetica"), 7, QFont::Bold, false));
0064     painter.drawText(2, 1, height() - 2, height() - 1, Qt::AlignCenter, QStringLiteral("x"));
0065 }
0066 
0067 CollectionWidget::CollectionWidget(Collection *collection, WordCount *dueWords, QWidget *parent)
0068     : QWidget(parent)
0069     , m_collection(collection)
0070 {
0071     setupWidget(dueWords);
0072     fillWidget();
0073 }
0074 
0075 CollectionWidget::~CollectionWidget()
0076 {
0077 }
0078 
0079 Collection *CollectionWidget::collection() const
0080 {
0081     return m_collection;
0082 }
0083 
0084 void CollectionWidget::setCollection(Collection *collection)
0085 {
0086     m_collection = collection;
0087 }
0088 
0089 void CollectionWidget::updateDue()
0090 {
0091     WordCount due;
0092     m_collection->numDueWords(due);
0093     m_barWidget->setDue(due);
0094 
0095     if (due.totalWords == 0 /* && due->percentageCompleted < 100*/) {
0096         m_practiceButton->setText(i18n("Practice Anyway"));
0097     } else {
0098         m_practiceButton->setText(i18n("Practice"));
0099     }
0100 }
0101 
0102 // ----------------------------------------------------------------
0103 //                         private classes
0104 
0105 void CollectionWidget::setupWidget(WordCount *dueWords)
0106 {
0107     // Set a nice shadow effect.
0108     QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect();
0109     effect->setBlurRadius(50);
0110     this->setGraphicsEffect(effect);
0111     QPalette palette = this->palette();
0112     palette.setColor(QPalette::Window, Qt::white);
0113     this->setAutoFillBackground(true);
0114     this->setPalette(palette);
0115 
0116     // Fill in the contents of the widget.
0117 
0118     // mainLayout is the main vertical layout for one collection widget.
0119     QVBoxLayout *mainLayout = new QVBoxLayout();
0120     mainLayout->setAlignment(Qt::AlignCenter);
0121     this->setLayout(mainLayout);
0122 
0123     // One collection is laid out vertically like this:
0124     //  1. titleLabel:   contains the title of the collection
0125     //  2. thumbnail:    an image that represents the collection. It could be a
0126     //                   wordcloud generated from the words in the collection, a
0127     //                   logo or something else.
0128     //  3. barWidget:    a visual bar showing the training status of the words in
0129     //                   the collection
0130     //  4. buttonLayout: a horizontal row of pushbuttons for delete, practice, etc
0131     m_titleLabel = new QLabel(this);
0132     mainLayout->addWidget(m_titleLabel);
0133 
0134     m_thumbnail = new QLabel(this);
0135     m_thumbnail->setFixedSize(COLLWIDTH - 10, COLLHEIGHT1 - COLLHEIGHT2 + 10);
0136     QPixmap pixmap(m_thumbnail->size());
0137     pixmap.fill(Qt::lightGray);
0138     m_thumbnail->setPixmap(pixmap);
0139 
0140     int percentageCompleted = dueWords->percentageCompleted();
0141     if (percentageCompleted != 100) {
0142         mainLayout->addWidget(m_thumbnail);
0143     }
0144 
0145     m_barWidget = new BarWidget(dueWords, this);
0146     m_barWidget->setFixedSize(COLLWIDTH - 10, 45);
0147     mainLayout->addWidget(m_barWidget);
0148     m_practiceButton = new QPushButton(this);
0149     QString buttonStyleSheet = "QPushButton { border: none; margin: 0px; padding: 0px; color: " + palette.color(QPalette::Active, QPalette::ButtonText).name()
0150         + "; background-color: " + palette.color(QPalette::Active, QPalette::Button).name() + "; border-radius: 3px;}";
0151     m_practiceButton->setStyleSheet(buttonStyleSheet);
0152 
0153     // buttonLayout is the horizontal layout for the bottom line in the
0154     // collection widget: delete button, practice button, etc
0155     QHBoxLayout *buttonLayout = new QHBoxLayout();
0156     mainLayout->addLayout(buttonLayout);
0157     m_removeButton = new RemoveButton(this);
0158     m_removeButton->setFixedSize(20, 20);
0159     buttonLayout->setAlignment(m_removeButton, Qt::AlignLeft | Qt::AlignVCenter);
0160     buttonLayout->setAlignment(m_practiceButton, Qt::AlignCenter);
0161     buttonLayout->addWidget(m_removeButton);
0162     buttonLayout->addWidget(m_practiceButton);
0163     buttonLayout->addItem(new QSpacerItem(20, 20));
0164 
0165     connect(m_practiceButton, &QPushButton::clicked, this, &CollectionWidget::practiceButtonClicked);
0166     connect(m_removeButton, &RemoveButton::clicked, this, &CollectionWidget::removeButtonClicked);
0167 }
0168 
0169 void CollectionWidget::fillWidget()
0170 {
0171     m_titleLabel->setText(m_collection->eduVocDocument()->title());
0172 
0173     updateDue();
0174 }
0175 
0176 #include "moc_collectionwidget.cpp"