Просмотр исходного кода

Implemented Galadriel widget tooltip functionality

Ivan Arkhipov 4 лет назад
Родитель
Сommit
6541f11b39

+ 66 - 56
src/Legacy/widgets/statuswidget.cpp

@@ -11,6 +11,7 @@
 #include <QUrl>
 #include <QDebug>
 #include <QMessageBox>
+#include <QRandomGenerator>
 
 StatusWidget::StatusWidget(PatchList *legacy_patches, QWidget *parent)
     : QWidget(parent)
@@ -19,6 +20,7 @@ StatusWidget::StatusWidget(PatchList *legacy_patches, QWidget *parent)
 {
     ui->setupUi(this);
     last_statusbar_update_time_.start();
+    ui->galadriel_tooltip_example->hide();
 
     connect(ui->weekly_code_widget, &WeeklyCodeWidget::showCompletedTooltip, this, [this](){setToolTipMessage("ЕЖЕНЕДЕЛЬНЫЙ КОД СКОПИРОВАН В БУФЕР ОБМЕНА", E_INFO);});
     connect(ui->weekly_code_widget, &WeeklyCodeWidget::showHelpTooltip, this, [this](){setToolTipMessage("КЛИКНИТЕ ПО ЕЖЕНЕДЕЛЬНОМУ КОДУ, ЧТОБЫ СКОПИРОВАТЬ ЕГО", E_INFO);});
@@ -30,26 +32,6 @@ StatusWidget::StatusWidget(PatchList *legacy_patches, QWidget *parent)
     connect(ui->server_status_widget, &ServerStatusWidget::showServersTooltip, this, [this](QString message){setToolTipMessage(message, E_INFO);});
     connect(ui->server_status_widget, &ServerStatusWidget::showNoTooltip, this, [this](){unsetToolTipMessage(E_INFO);});
 
-    tooltip_effects[ui->galadriel_tooltip_1->objectName()] = new QGraphicsOpacityEffect(ui->galadriel_tooltip_1);
-    tooltip_effects[ui->galadriel_tooltip_2->objectName()] = new QGraphicsOpacityEffect(ui->galadriel_tooltip_2);
-
-    tooltip_animations[ui->galadriel_tooltip_1->objectName()] = new QPropertyAnimation(tooltip_effects[ui->galadriel_tooltip_1->objectName()], "opacity");
-    tooltip_animations[ui->galadriel_tooltip_2->objectName()] = new QPropertyAnimation(tooltip_effects[ui->galadriel_tooltip_2->objectName()], "opacity");
-
-    ui->galadriel_tooltip_1->setGraphicsEffect(tooltip_effects[ui->galadriel_tooltip_1->objectName()]);
-    ui->galadriel_tooltip_2->setGraphicsEffect(tooltip_effects[ui->galadriel_tooltip_2->objectName()]);
-
-    foreach (QPropertyAnimation* animation, tooltip_animations) {
-        animation->setDuration(350);
-        animation->setStartValue(0);
-        animation->setEndValue(1);
-        animation->setDirection(QAbstractAnimation::Forward);
-    }
-
-    foreach (QGraphicsOpacityEffect* effect, tooltip_effects) {
-        effect->setOpacity(0);
-    }
-
     connect(legacy_patches_, &PatchList::patchOperationsStarted, this, &StatusWidget::onPatchTotalOperationsStarted);
     connect(legacy_patches_, &PatchList::patchOperationsFinished, this, &StatusWidget::onPatchTotalOperationsFinished);
     connect(legacy_patches_, &PatchList::progressChanged, this, &StatusWidget::onPatchTotalProgressChanged);
@@ -59,6 +41,11 @@ StatusWidget::StatusWidget(PatchList *legacy_patches, QWidget *parent)
         connect(patch, &Patch::operationStarted, this, &StatusWidget::onPatchOperationStarted);
         connect(patch, &Patch::operationFinished, this, &StatusWidget::onPatchOperationFinished);
     }
+
+    generateRandomTooltipMessage();
+    random_tooltip_generator_timer_.setInterval(5 * 1000);
+    connect(&random_tooltip_generator_timer_, &QTimer::timeout, this, &StatusWidget::generateRandomTooltipMessage);
+    random_tooltip_generator_timer_.start();
 }
 
 StatusWidget::~StatusWidget()
@@ -76,7 +63,10 @@ void StatusWidget::setToolTipMessage(QString message, StatusWidget::ToolTipState
 {
     tooltip_messages_[state] = message;
     tooltip_state_ = ToolTipState(int(tooltip_state_) | int(state));
+    qDebug() << "New tooltip state: " << int(tooltip_state_);
+
     if (tooltip_state_ < state * 2) {
+        qDebug() << "Fading between tooltips";
         fadeBetweenToolTips(message);
     }
 }
@@ -84,6 +74,7 @@ void StatusWidget::setToolTipMessage(QString message, StatusWidget::ToolTipState
 void StatusWidget::unsetToolTipMessage(StatusWidget::ToolTipState state)
 {
     tooltip_state_ = ToolTipState(int(tooltip_state_) & (~int(state)));
+    qDebug() << "New tooltip state: " << int(tooltip_state_);
 
     if (tooltip_state_ & E_INFO) {
         fadeBetweenToolTips(tooltip_messages_[E_INFO]);
@@ -131,47 +122,45 @@ void StatusWidget::resizeEvent(QResizeEvent *)
     updateFontsSizes();
 }
 
-void StatusWidget::fadeBetweenToolTips(QString next_tooltip_message)
+void StatusWidget::generateRandomTooltipMessage()
 {
-    if (!galadriel_tooltip_1_type) {
-        // Previous state:
-        // tooltip1 - hiding, tooltip2 - showing.
-        galadriel_tooltip_1_type = true;
-
-        ui->galadriel_tooltip_1->setText(next_tooltip_message);
-
-        qDebug() << "StatusWidget: Showing message: " << ui->galadriel_tooltip_1->text();
-        qDebug() << "StatusWidget: Hiding message: " << ui->galadriel_tooltip_2->text();
-
-        QPropertyAnimation* showing_anim = tooltip_animations[ui->galadriel_tooltip_1->objectName()];
-        showing_anim->setDirection(QAbstractAnimation::Forward);
-        if (showing_anim->state() == QAbstractAnimation::Stopped)
-            showing_anim->start();
-
-        QPropertyAnimation* hiding_anim = tooltip_animations[ui->galadriel_tooltip_2->objectName()];
-        hiding_anim->setDirection(QAbstractAnimation::Backward);
-        if (hiding_anim->state() == QAbstractAnimation::Stopped)
-            hiding_anim->start();
-    } else {
-        // Previous state:
-        // tooltip1 - showing, tooltip2 - hiding.
-        galadriel_tooltip_1_type = false;
-
-        ui->galadriel_tooltip_2->setText(next_tooltip_message);
+    quint32 number = QRandomGenerator::system()->generate();
+    setToolTipMessage("СЛУЧАЙНОЕ СООБЩЕНИЕ №" + QString::number(number), E_RANDOM);
+}
 
-        qDebug() << "StatusWidget: Showing message: " << ui->galadriel_tooltip_2->text();
-        qDebug() << "StatusWidget: Hiding message: " << ui->galadriel_tooltip_1->text();
+void StatusWidget::fadeBetweenToolTips(QString next_tooltip_message)
+{
+    if (next_tooltip_message == current_tooltip_message_) {
+        return;
+    }
 
-        QPropertyAnimation* showing_anim = tooltip_animations[ui->galadriel_tooltip_2->objectName()];
-        showing_anim->setDirection(QAbstractAnimation::Forward);
-        if (showing_anim->state() == QAbstractAnimation::Stopped)
-            showing_anim->start();
+    if (!tooltip_widgets_.contains(next_tooltip_message)) {
+        createTooltipMessageWidget(next_tooltip_message);
+    }
 
-        QPropertyAnimation* hiding_anim = tooltip_animations[ui->galadriel_tooltip_1->objectName()];
+    QPropertyAnimation* showing_anim = tooltip_animations_[next_tooltip_message];
+    showing_anim->setDirection(QAbstractAnimation::Forward);
+    if (showing_anim->state() == QAbstractAnimation::Stopped)
+        showing_anim->start();
+
+    if (tooltip_widgets_.contains(current_tooltip_message_)) {
+        QString msg = current_tooltip_message_;
+        connect(tooltip_animations_[current_tooltip_message_], &QPropertyAnimation::finished, this, [&, msg](){
+            if (tooltip_effects_.contains(msg) && tooltip_effects_[msg]->opacity() == 0) {
+                qDebug() << "Deleting " << msg;
+                tooltip_widgets_.take(msg)->deleteLater();
+                tooltip_effects_.take(msg)->deleteLater();
+                tooltip_animations_.take(msg)->deleteLater();
+            }
+        });
+
+        QPropertyAnimation* hiding_anim = tooltip_animations_[current_tooltip_message_];
         hiding_anim->setDirection(QAbstractAnimation::Backward);
         if (hiding_anim->state() == QAbstractAnimation::Stopped)
             hiding_anim->start();
     }
+
+    current_tooltip_message_ = next_tooltip_message;
 }
 
 void StatusWidget::onPatchTotalOperationsStarted()
@@ -301,7 +290,6 @@ void StatusWidget::onPatchProgressChanged(Patch::OperationProgress progress, Pat
 //    }
 }
 
-
 void StatusWidget::updateStatusBar(Patch::OperationProgress progress)
 {
 //    if (last_statusbar_update_time_.elapsed() > 500) {
@@ -321,10 +309,32 @@ void StatusWidget::updateStatusBar(Patch::OperationProgress progress)
 
 //        ui->progress_label->setText(text);
 //        last_statusbar_update_time_.restart();
-//    }
+    //    }
 }
 
-
+void StatusWidget::createTooltipMessageWidget(QString tooltip_message)
+{
+    qDebug() << "Creating:" << tooltip_message;
+    tooltip_widgets_[tooltip_message] = new QLabel(ui->galadriel_widget);
+    tooltip_widgets_[tooltip_message]->setText(tooltip_message);
+    tooltip_widgets_[tooltip_message]->setGeometry(ui->galadriel_tooltip_example->geometry());
+    tooltip_widgets_[tooltip_message]->setStyleSheet(ui->galadriel_tooltip_example->styleSheet());
+    tooltip_widgets_[tooltip_message]->setFont(ui->galadriel_tooltip_example->font());
+    tooltip_widgets_[tooltip_message]->setWordWrap(true);
+    tooltip_widgets_[tooltip_message]->setAlignment(Qt::AlignCenter);
+    tooltip_widgets_[tooltip_message]->show();
+
+    tooltip_effects_[tooltip_message] = new QGraphicsOpacityEffect(tooltip_widgets_[tooltip_message]);
+    tooltip_effects_[tooltip_message]->setOpacity(0);
+
+    tooltip_animations_[tooltip_message] = new QPropertyAnimation(tooltip_effects_[tooltip_message], "opacity");
+    tooltip_animations_[tooltip_message]->setDuration(350);
+    tooltip_animations_[tooltip_message]->setStartValue(0);
+    tooltip_animations_[tooltip_message]->setEndValue(1);
+    tooltip_animations_[tooltip_message]->setDirection(QAbstractAnimation::Forward);
+
+    tooltip_widgets_[tooltip_message]->setGraphicsEffect(tooltip_effects_[tooltip_message]);
+}
 
 void StatusWidget::on_check_for_updates_button_clicked()
 {

+ 12 - 8
src/Legacy/widgets/statuswidget.h

@@ -22,10 +22,10 @@ class StatusWidget : public QWidget
 public:
     // States. Higher state value means higher priority on visualisation
     enum ToolTipState : int {
-        E_INFO = 4,
-        E_ERROR = 2,
-        E_PROCESS = 1,
-        E_RANDOM = 0 // (state | E_RANDOM) should be always true (we can always show random message)
+        E_INFO = 8,
+        E_ERROR = 4,
+        E_PROCESS = 2,
+        E_RANDOM = 1
     };
 
     explicit StatusWidget(PatchList *legacy_patches, QWidget *parent = 0);
@@ -42,6 +42,8 @@ protected:
     void resizeEvent(QResizeEvent *event) override;
 
 private slots:
+    void generateRandomTooltipMessage();
+
     void fadeBetweenToolTips(QString tooltip_message);
 
     void onPatchTotalOperationsStarted();
@@ -60,6 +62,7 @@ private slots:
 private:
     void updateStatusBar(Patch::OperationProgress progress);
 
+    void createTooltipMessageWidget(QString tooltip_message);
 private:
     Ui::StatusWidget *ui;
 
@@ -67,10 +70,10 @@ private:
 
     bool all_patch_operations_finished_ = false;
 
-    QMap<QString, QGraphicsOpacityEffect*> tooltip_effects;
-    QMap<QString, QPropertyAnimation*> tooltip_animations;
-
-    bool galadriel_tooltip_1_type = false; // false = hiding, true - showing. Tooltip 2 is always opposite to tooltip 1
+    QMap<QString, QLabel*> tooltip_widgets_;
+    QMap<QString, QGraphicsOpacityEffect*> tooltip_effects_;
+    QMap<QString, QPropertyAnimation*> tooltip_animations_;
+    QString current_tooltip_message_;
 
     QMap<Patch*, Patch::Operation> patch_operations;
 
@@ -79,6 +82,7 @@ private:
     ToolTipState tooltip_state_ = E_RANDOM;
     QMap<ToolTipState, QString> tooltip_messages_;
 
+    QTimer random_tooltip_generator_timer_;
 };
 
 #endif // STATUSWIDGET_H

+ 4 - 30
src/Legacy/widgets/statuswidget.ui

@@ -157,35 +157,6 @@ QScrollBar:vertical {
 border-image: url(:/characters/galadriel_with_text.png);
 }</string>
    </property>
-   <widget class="QLabel" name="galadriel_tooltip_2">
-    <property name="geometry">
-     <rect>
-      <x>29</x>
-      <y>17</y>
-      <width>371</width>
-      <height>114</height>
-     </rect>
-    </property>
-    <property name="font">
-     <font>
-      <family>EB Garamond</family>
-      <pointsize>12</pointsize>
-      <italic>false</italic>
-     </font>
-    </property>
-    <property name="styleSheet">
-     <string notr="true"/>
-    </property>
-    <property name="text">
-     <string>TEST TOOLTIP USED FOR STYLE DESC</string>
-    </property>
-    <property name="alignment">
-     <set>Qt::AlignCenter</set>
-    </property>
-    <property name="wordWrap">
-     <bool>true</bool>
-    </property>
-   </widget>
    <widget class="QToolButton" name="check_for_updates_button">
     <property name="geometry">
      <rect>
@@ -202,7 +173,10 @@ border-image: url(:/characters/galadriel_with_text.png);
      <string/>
     </property>
    </widget>
-   <widget class="QLabel" name="galadriel_tooltip_1">
+   <widget class="QLabel" name="galadriel_tooltip_example">
+    <property name="enabled">
+     <bool>false</bool>
+    </property>
     <property name="geometry">
      <rect>
       <x>29</x>