importantmessageswidget.cpp
1 //------------------------------------------------------------------------------
2 // importantmessageswidget.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2011 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "importantmessageswidget.h"
24 
25 #include <QDateTime>
26 #include "ui_importantmessageswidget.h"
27 #include <QLabel>
28 #include <QVBoxLayout>
29 
30 DClass<ImportantMessagesWidget> : public Ui::ImportantMessagesWidget
31 {
32 public:
33  class MessageLabel
34  {
35  public:
36  MessageLabel(QLabel* pLabel)
37  {
38  this->pLabel = pLabel;
39  this->timeCreated = QDateTime::currentDateTime();
40  }
41 
42  QLabel* pLabel;
43  QDateTime timeCreated;
44  };
45 
46  static const unsigned DEFAULT_MAX_MESSAGES = 0;
47  static const int MAX_MSG_KEEP_TIME_SEC = 10;
48 
49  QList<MessageLabel> labelWidgets;
50  int maxMessages;
51 };
52 
53 DPointered(ImportantMessagesWidget)
54 
56 : QWidget(pParent)
57 {
59  d->setupUi(this);
60 
61  this->hide();
62 }
63 
64 ImportantMessagesWidget::~ImportantMessagesWidget()
65 {
66 }
67 
68 void ImportantMessagesWidget::addMessage(const QString& message)
69 {
70  addMessage(message, QDateTime());
71 }
72 
73 void ImportantMessagesWidget::addMessage(const QString& message, const QDateTime& dateTime)
74 {
75  QString strTimestamp = "";
76  if (dateTime.isValid())
77  {
78  strTimestamp = dateTime.toString("[hh:mm:ss]") + " ";
79  }
80 
81  QString formattedMessage = "<p>" + strTimestamp + message + "</p>";
82 
83  QLabel* pNewLabel = new QLabel(this);
84  pNewLabel->setText(formattedMessage);
85  pNewLabel->setTextInteractionFlags(pNewLabel->textInteractionFlags()
86  | Qt::TextSelectableByMouse);
87  pNewLabel->setCursor(Qt::IBeamCursor);
88  pNewLabel->setWordWrap(true);
89 
90  d->labelWidgets << PrivData<ImportantMessagesWidget>::MessageLabel(pNewLabel);
91  d->messageLayout->addWidget(pNewLabel);
92 
93  // Remember that widget may be auto-hidden.
94  this->show();
95 
96  // Do not forget co clear oldest widgets if necessary.
97  dropOldWidgetsIfBeyondLimit();
98 }
99 
100 void ImportantMessagesWidget::addMessage(const QString& message, unsigned timestamp)
101 {
102  addMessage(message, QDateTime::fromTime_t(timestamp));
103 }
104 
106 {
107  while (!d->labelWidgets.isEmpty())
108  {
109  removeOneOldest();
110  }
111 }
112 
113 void ImportantMessagesWidget::dropOldWidgetsIfBeyondLimit()
114 {
115  while (d->labelWidgets.size() > d->maxMessages)
116  {
117  PrivData<ImportantMessagesWidget>::MessageLabel& oldestLabel = d->labelWidgets.first();
118  int timeDifference = oldestLabel.timeCreated.secsTo(QDateTime::currentDateTime());
119 
121  {
122  removeOneOldest();
123  }
124  else
125  {
126  // Exit the loop as no message is old enough to be dropped.
127  // Messages are ordered in chronological order.
128  break;
129  }
130  }
131 }
132 
134 {
135  if (num >= d->labelWidgets.size())
136  {
137  clear();
138  }
139  else
140  {
141  for (; num > 0; --num)
142  {
143  removeOneOldest();
144  }
145  }
146 }
147 
149 {
150  if (!d->labelWidgets.isEmpty())
151  {
152  delete d->labelWidgets.takeFirst().pLabel;
153 
154  if (d->labelWidgets.isEmpty())
155  {
156  // Auto-hide when there is nothing to show.
157  hide();
158  }
159  }
160 }
161 
163 {
164  return d->maxMessages;
165 }
166 
167 void ImportantMessagesWidget::setMaxMessages(unsigned num)
168 {
169  d->maxMessages = num;
170 }
void addMessage(const QString &message)
Adds a new message without a timestamp.
void removeOneOldest()
Will remove one oldest message.
A self-scaling widget that displays messages as separate instances of QLabel.
unsigned maxMessages() const
Amount of messages that can be displayed in the widget at the same time.
void clear()
Clears all messages from the widghet.
Definition: dptr.h:31
void removeOldest(int num)
Removes a number of oldest messages from the widget.