1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4     Rosegarden
5     A MIDI and audio sequencer and musical notation editor.
6     Copyright 2000-2021 the Rosegarden development team.
7 
8     Other copyrights also apply to some parts of this work.  Please
9     see the AUTHORS file and individual file headers for details.
10 
11     This program is free software; you can redistribute it and/or
12     modify it under the terms of the GNU General Public License as
13     published by the Free Software Foundation; either version 2 of the
14     License, or (at your option) any later version.  See the file
15     COPYING included with this distribution for more information.
16 */
17 
18 #define RG_MODULE_STRING "[WarningWidget]"
19 
20 #include "WarningWidget.h"
21 
22 #include "gui/general/IconLoader.h"
23 #include "misc/Strings.h"
24 #include "misc/Debug.h"
25 
26 #include <QWidget>
27 #include <QLabel>
28 #include <QHBoxLayout>
29 #include <QToolButton>
30 #include <QMessageBox>
31 
32 #include <iostream>
33 namespace Rosegarden
34 {
35 
WarningWidget(QWidget * parent)36 WarningWidget::WarningWidget(QWidget *parent) :
37     QWidget(parent),
38     m_text(""),
39     m_informativeText(""),
40     m_warningDialog(new WarningDialog(parent))
41 {
42     setContentsMargins(0, 0, 0, 0);
43 
44     setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
45     QHBoxLayout *layout = new QHBoxLayout();
46     setLayout(layout);
47     layout->setContentsMargins(0, 0, 0, 0);
48     layout->setSpacing(2);
49 
50     m_midiIcon = new QLabel();
51     layout->addWidget(m_midiIcon);
52 
53     m_audioIcon = new QLabel();
54     layout->addWidget(m_audioIcon);
55 
56     m_timerIcon = new QLabel();
57     layout->addWidget(m_timerIcon);
58 
59     m_warningButton = new QToolButton();
60     layout->addWidget(m_warningButton);
61     m_warningButton->setIconSize(QSize(16, 16));
62     m_warningButton->setIcon(IconLoader::loadPixmap("warning"));
63     connect(m_warningButton,
64             &QAbstractButton::clicked,
65             this,
66             &WarningWidget::displayMessageQueue);
67     m_warningButton->setToolTip(tr("<qt><p>Performance problems detected!</p><p>Click to display details</p></qt>"));
68     m_warningButton->hide();
69 
70     m_infoButton = new QToolButton();
71     layout->addWidget(m_infoButton);
72     m_infoButton->setIconSize(QSize(16, 16));
73     m_infoButton->setIcon(IconLoader::loadPixmap("messagebox-information"));
74     connect(m_infoButton,
75             &QAbstractButton::clicked,
76             this,
77             &WarningWidget::displayMessageQueue);
78     m_infoButton->setToolTip(tr("<qt><p>Information available.</p><p>Click to display details</p></qt>"));
79     m_infoButton->hide();
80 
81     // Set these to false initially, assuming an all clear state.  When some
82     // problem crops up, these will be set true as appropriate by
83     // RosegardenMainWindow, which manages this widget
84     setMidiWarning(false);
85     setAudioWarning(false);
86     setTimerWarning(false);
87 }
88 
~WarningWidget()89 WarningWidget::~WarningWidget()
90 {
91 }
92 
93 void
setMidiWarning(const bool status)94 WarningWidget::setMidiWarning(const bool status)
95 {
96     if (status) {
97         m_midiIcon->hide();
98     } else {
99         m_midiIcon->setPixmap(IconLoader::loadPixmap("midi-ok"));
100         m_midiIcon->show();
101         m_midiIcon->setToolTip(tr("MIDI OK"));
102     }
103 }
104 
105 void
setAudioWarning(const bool status)106 WarningWidget::setAudioWarning(const bool status)
107 {
108     if (status) {
109         m_audioIcon->hide();
110     } else {
111         m_audioIcon->setPixmap(IconLoader::loadPixmap("audio-ok"));
112         m_audioIcon->show();
113         m_audioIcon->setToolTip(tr("audio OK"));
114     }
115 }
116 
117 void
setTimerWarning(const bool status)118 WarningWidget::setTimerWarning(const bool status)
119 {
120     if (status) {
121         m_timerIcon->hide();
122     } else {
123         m_timerIcon->setPixmap(IconLoader::loadPixmap("timer-ok"));
124         m_timerIcon->show();
125         m_timerIcon->setToolTip(tr("timer OK"));
126     }
127 }
128 
129 void
queueMessage(const int type,const QString text,const QString informativeText)130 WarningWidget::queueMessage(const int type, const QString text, const QString informativeText)
131 {
132     RG_DEBUG << "WarningWidget::queueMessage(" << text
133              << ", " << informativeText << ")";
134 
135     // we'll go ahead and splay this out in a big'ol switch in case there are
136     // ever other warning types that have special icons
137     switch (type) {
138     case Info:
139         m_infoButton->show();
140         break;
141     case Midi:
142     case Audio:
143     case Timer:
144     case Other:
145     default:
146         m_warningButton->show();
147     }
148 
149     // this is all a bit awkard, but there's no std::triplet and I don't want to
150     // convert this all over to a vector or something, so I just nested a
151     // std::pair in a std::pair, and I can't be bothered to typedef the
152     // sub-component bit here
153     std::pair<QString, QString> m;
154     m.first = text;
155     m.second = informativeText;
156 
157     Message message(m, type);
158 
159     m_queue.enqueue(message);
160 }
161 
162 void
displayMessageQueue()163 WarningWidget::displayMessageQueue()
164 {
165     while (!m_queue.isEmpty()) {
166         std::cerr << " - emptying queue..." << std::endl;
167         m_warningDialog->addWarning(m_queue.dequeue());
168     }
169     m_warningDialog->show();
170 }
171 
172 // NOTES:
173 // Potential future warnings:
174 //
175 // * Chris's newly refactored autoconnect logic couldn't find a plausible looking
176 // thing to talk to, so you probably need to run QSynth now &c.
177 //
178 }
179