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 "ui_importantmessageswidget.h"
26 #include <QDateTime>
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  strTimestamp = dateTime.toString("[hh:mm:ss]") + " ";
78 
79  QString formattedMessage = "<p>" + strTimestamp + message + "</p>";
80 
81  QLabel *pNewLabel = new QLabel(this);
82  pNewLabel->setText(formattedMessage);
83  pNewLabel->setTextInteractionFlags(pNewLabel->textInteractionFlags()
84  | Qt::TextSelectableByMouse);
85  pNewLabel->setCursor(Qt::IBeamCursor);
86  pNewLabel->setWordWrap(true);
87 
88  d->labelWidgets << PrivData<ImportantMessagesWidget>::MessageLabel(pNewLabel);
89  d->messageLayout->addWidget(pNewLabel);
90 
91  // Remember that widget may be auto-hidden.
92  this->show();
93 
94  // Do not forget co clear oldest widgets if necessary.
95  dropOldWidgetsIfBeyondLimit();
96 }
97 
98 void ImportantMessagesWidget::addMessage(const QString &message, unsigned timestamp)
99 {
100  addMessage(message, QDateTime::fromTime_t(timestamp));
101 }
102 
104 {
105  while (!d->labelWidgets.isEmpty())
106  {
107  removeOneOldest();
108  }
109 }
110 
111 void ImportantMessagesWidget::dropOldWidgetsIfBeyondLimit()
112 {
113  while (d->labelWidgets.size() > d->maxMessages)
114  {
115  PrivData<ImportantMessagesWidget>::MessageLabel &oldestLabel = d->labelWidgets.first();
116  int timeDifference = oldestLabel.timeCreated.secsTo(QDateTime::currentDateTime());
117 
119  removeOneOldest();
120  else
121  {
122  // Exit the loop as no message is old enough to be dropped.
123  // Messages are ordered in chronological order.
124  break;
125  }
126  }
127 }
128 
130 {
131  if (num >= d->labelWidgets.size())
132  clear();
133  else
134  {
135  for (; num > 0; --num)
136  removeOneOldest();
137  }
138 }
139 
141 {
142  if (!d->labelWidgets.isEmpty())
143  {
144  delete d->labelWidgets.takeFirst().pLabel;
145 
146  if (d->labelWidgets.isEmpty())
147  {
148  // Auto-hide when there is nothing to show.
149  hide();
150  }
151  }
152 }
153 
155 {
156  return d->maxMessages;
157 }
158 
159 void ImportantMessagesWidget::setMaxMessages(unsigned num)
160 {
161  d->maxMessages = num;
162 }
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.