summaryrefslogtreecommitdiff
path: root/src/ui/osdmessage.cpp
blob: 4099454d2f048b58f4f753fe0e7f7295592afeac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// SPDX-License-Identifier: GPL-2.0-or-later

#include <QPaintEvent>
#include <QCursor>
#include <QTimer>
#include <QApplication>
#include <QPainter>
#include <QBitmap>
#include <QPalette>

#include "fqterm_trace.h"
#include "fqterm_path.h"

#include "osdmessage.h"

namespace FQTerm {

PageViewMessage::PageViewMessage(QWidget *parent_)
    : QWidget(parent_),
      timer_(0),
      message_() {
  setFocusPolicy(Qt::NoFocus);
  //setBackgroundMode( Qt::NoBackground );
  QPalette palette;
  palette.setColor(backgroundRole(), qApp->palette().color(QPalette::Active,
                                                           QPalette::Background));
  setPalette(palette);
  //     setPaletteBackgroundColor(qApp->palette().color(QPalette::Active, QPalette::Background));
  setCursor(QCursor(Qt::ArrowCursor));
  move(10, 10);
  resize(0, 0);
  hide();
}

// give to Caesar what Caesar owns: code taken from Amarok's osd.h/.cpp
// "redde (reddite, pl.) cesari quae sunt cesaris", just btw. ;)
void PageViewMessage::display(const QString &message, Icon icon, int durationMs, QPoint pos, PageViewMessage::Alignment ali)
{
  displayImpl(message, icon, false, durationMs, pos, ali);
}

void PageViewMessage::display(const QString &message, QPoint pos, Icon icon)
{
  displayImpl(message, icon, false, 4000, pos, TopLeft);
}

QRect PageViewMessage::displayCheck(const QString &message, Icon icon)
{
  return displayImpl(message, icon, true);
}

QRect PageViewMessage::displayImpl(const QString &message, Icon icon, bool check, int durationMs, QPoint pos, Alignment ali)
{
  //FIXME: add a option in fqterm too.
  /*
  if ( !KpdfSettings::showOSD() )
  {
  hide();
  return;
  }
  */
  // determine text rectangle

  if (!isHidden() && message == message_) {
    timer_->setSingleShot(true);
    timer_->start(durationMs);
    return QRect(QPoint(0, 0), size());
  }
  message_ = message;
  QRect textRect = fontMetrics().boundingRect(message);
  textRect.translate(-textRect.left(), -textRect.top());
  textRect.adjust(0, 0, 2, 2);
  int width = textRect.width(), height = textRect.height(), textXOffset = 0,
    shadowOffset = message.isRightToLeft() ? -1: 1;

  // load icon (if set) and update geometry
  // [FQTerm], we don't have a icon at this time.

  QPixmap symbol;
  if (icon != None) {
    switch (icon) {
      //case Find:
      //symbol = SmallIcon( "viewmag" );
      //break;
      case Error:
        symbol = QPixmap(getPath(RESOURCE) + "pic/messagebox_critical.png");
        break;
      case Warning:
        symbol = QPixmap(getPath(RESOURCE) + "pic/messagebox_warning.png");
        break;
      default:
        symbol = QPixmap(getPath(RESOURCE) + "pic/messagebox_info.png");
        break;
    }
    textXOffset = 2+symbol.width();
    width += textXOffset;
    height = qMax(height, symbol.height());
  }

  QRect geometry(0, 0, width + 10, height + 8);

  if (check)
    return geometry;

  // resize pixmap, mask and widget
  static QPixmap mask;
  mask = QPixmap(geometry.size());
  pixmap_ = QPixmap(geometry.size());
  resize(geometry.size());

  switch(ali)
  {
  case TopLeft:
    move(pos);
    break;
  case TopRight:
    move(pos - geometry.topRight());
    break;
  case BottomLeft:
    move(pos - geometry.bottomLeft());
    break;
  case BottomRight:
    move(pos - geometry.bottomRight());
    break;
  }

  // create and set transparency mask
  QPainter maskPainter(&mask);
  mask.fill(Qt::black);
  maskPainter.setBrush(Qt::white);
  maskPainter.drawRoundRect(geometry, 1600 / geometry.width(), 1600 /
    geometry.height());
  setMask(mask.createHeuristicMask());

  // draw background
  QPainter bufferPainter(&pixmap_);
  bufferPainter.setPen(Qt::black);
  bufferPainter.setBrush(palette().brush(QPalette::Window));
  bufferPainter.drawRoundRect(geometry, 1600 / geometry.width(), 1600 /
    geometry.height());

  // draw icon if present
  if (!symbol.isNull()) {
    bufferPainter.drawPixmap(5, 4, symbol, 0, 0, symbol.width(), symbol.height());
  }

  // draw shadow and text
  int yText = geometry.height() - height / 2;
  bufferPainter.setPen(palette().color(QPalette::Window).dark(115));
  bufferPainter.drawText(5+textXOffset + shadowOffset, yText + 1, message);
  bufferPainter.setPen(palette().color(QPalette::WindowText));
  bufferPainter.drawText(5+textXOffset, yText, message);

  // show widget and schedule a repaint
  show();
  update();

  // close the message window after given mS
  if (durationMs > 0) {
    if (!timer_) {
      timer_ = new QTimer(this);
      timer_->setSingleShot(true);
      FQ_VERIFY(connect(timer_, SIGNAL(timeout()), SLOT(hide())));
    }
    timer_->start(durationMs);
  } else if (timer_) {
    timer_->stop();
  }

  return QRect(QPoint(0, 0), size());
}

void PageViewMessage::paintEvent(QPaintEvent *e) {
  QPainter p(this);
  p.drawPixmap(e->rect().topLeft(), pixmap_, e->rect());
}

void PageViewMessage::mousePressEvent(QMouseEvent * /*e*/) {
  if (timer_) {
    timer_->stop();
  }
  hide();
}

void PageViewMessage::hideEvent( QHideEvent * event ) {
  emit hideAt(geometry ());
  QWidget::hideEvent(event);
}

}  // namespace FQTerm

#include "osdmessage.moc"