Skip to content

Commit

Permalink
Show popup details only when needed
Browse files Browse the repository at this point in the history
Fixes #380
  • Loading branch information
hluk committed Sep 2, 2015
1 parent 8a34ee0 commit 97b59c6
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
7 changes: 4 additions & 3 deletions src/gui/mainwindow.cpp
Expand Up @@ -1231,7 +1231,7 @@ void MainWindow::showMessage(const QString &title, const QString &msg,
void MainWindow::showMessage(const QString &title, const QString &msg, ushort icon,
int msec, int notificationId)
{
notificationDaemon()->create(title, msg, icon, msec, notificationId);
notificationDaemon()->create(title, msg, icon, msec, true, notificationId);
}

void MainWindow::showClipboardMessage(const QVariantMap &data)
Expand All @@ -1240,8 +1240,9 @@ void MainWindow::showClipboardMessage(const QVariantMap &data)
if (data.isEmpty()) {
notificationDaemon()->removeNotification(clipboardNotificationId);
} else {
notificationDaemon()->create( data, m_options.clipboardNotificationLines, IconPaste,
m_options.itemPopupInterval * 1000, clipboardNotificationId );
notificationDaemon()->create(
data, m_options.clipboardNotificationLines, IconPaste,
m_options.itemPopupInterval * 1000, false, clipboardNotificationId );
}
}
}
Expand Down
27 changes: 17 additions & 10 deletions src/gui/notification.cpp
Expand Up @@ -40,7 +40,8 @@

namespace {

void showNotificationInspectDialog(const QString &messageTitle, const QString &message)
void showNotificationInspectDialog(
const QString &messageTitle, const QString &message, Qt::TextFormat format)
{
QScopedPointer<QDialog> dialog(new QDialog);
dialog->setObjectName("InspectNotificationDialog");
Expand All @@ -57,10 +58,12 @@ void showNotificationInspectDialog(const QString &messageTitle, const QString &m

QTextEdit *editor = new QTextEdit(dialog.data());
editor->setReadOnly(true);
editor->setText(
"<h3>" + escapeHtml(messageTitle) + "</h3>"
"<p>" + escapeHtml(message) + "</p>"
);
const QString title = escapeHtml(messageTitle);
const QString body = format == Qt::PlainText
? escapeHtml(message)
: message;
editor->setHtml( QString("<h3>%1</h3><p>%2</p>")
.arg(title, body) );

QDialogButtonBox *buttons = new QDialogButtonBox(
QDialogButtonBox::Close, Qt::Horizontal, dialog.data() );
Expand Down Expand Up @@ -139,9 +142,6 @@ void Notification::setMessage(const QString &msg, Qt::TextFormat format)
{
m_msgLabel->setTextFormat(format);
m_msgLabel->setText(msg);

m_textToCopy = (format == Qt::PlainText) ? msg : QString();
m_tipLabel->setVisible( !m_textToCopy.isEmpty() );
}

void Notification::setPixmap(const QPixmap &pixmap)
Expand Down Expand Up @@ -170,6 +170,11 @@ void Notification::setOpacity(qreal opacity)
setWindowOpacity(m_opacity);
}

void Notification::setClickToShowEnabled(bool enabled)
{
m_tipLabel->setVisible(enabled);
}

void Notification::updateIcon()
{
const QColor color = getDefaultIconColor(*this);
Expand All @@ -188,8 +193,10 @@ void Notification::adjust()

void Notification::mousePressEvent(QMouseEvent *event)
{
if (event->button() != Qt::LeftButton && !m_textToCopy.isEmpty() )
showNotificationInspectDialog(m_titleLabel->text(), m_textToCopy);
if ( event->button() != Qt::LeftButton && m_tipLabel->isVisible() ) {
showNotificationInspectDialog(
m_titleLabel->text(), m_msgLabel->text(), m_msgLabel->textFormat());
}

m_timer.stop();

Expand Down
2 changes: 1 addition & 1 deletion src/gui/notification.h
Expand Up @@ -39,6 +39,7 @@ class Notification : public QWidget
void setIcon(ushort icon);
void setInterval(int msec);
void setOpacity(qreal opacity);
void setClickToShowEnabled(bool enabled);

void updateIcon();

Expand Down Expand Up @@ -70,7 +71,6 @@ private slots:
QTimer m_timer;
qreal m_opacity;
ushort m_icon;
QString m_textToCopy;
};

#endif // NOTIFICATION_H
8 changes: 6 additions & 2 deletions src/gui/notificationdaemon.cpp
Expand Up @@ -54,19 +54,22 @@ NotificationDaemon::NotificationDaemon(QObject *parent)
initSingleShotTimer( &m_timerUpdate, 100, this, SLOT(doUpdateNotifications()) );
}

void NotificationDaemon::create(const QString &title, const QString &msg, ushort icon, int msec, int id)
void NotificationDaemon::create(
const QString &title, const QString &msg, ushort icon, int msec, bool clickToShow, int id)
{
Notification *notification = createNotification(id);

notification->setTitle(title);
notification->setIcon(icon);
notification->setMessage(msg);
notification->setInterval(msec);
notification->setClickToShowEnabled(clickToShow);

updateNotifications();
}

void NotificationDaemon::create(const QVariantMap &data, int maxLines, ushort icon, int msec, int id)
void NotificationDaemon::create(
const QVariantMap &data, int maxLines, ushort icon, int msec, bool clickToShow, int id)
{
Notification *notification = createNotification(id);

Expand Down Expand Up @@ -111,6 +114,7 @@ void NotificationDaemon::create(const QVariantMap &data, int maxLines, ushort ic
}

notification->setInterval(msec);
notification->setClickToShowEnabled(clickToShow);

updateNotifications();
}
Expand Down
10 changes: 6 additions & 4 deletions src/gui/notificationdaemon.h
Expand Up @@ -49,12 +49,14 @@ class NotificationDaemon : public QObject
explicit NotificationDaemon(QObject *parent = NULL);

/** Create new notification or update one with same @a id (if non-negative). */
void create(const QString &title, const QString &msg, ushort icon,
int msec, int id = -1);
void create(
const QString &title, const QString &msg, ushort icon,
int msec, bool clickToShow, int id = -1);

/** Create new notification or update one with same @a id (if non-negative). */
void create(const QVariantMap &data, int maxLines, ushort icon,
int msec, int id = -1);
void create(
const QVariantMap &data, int maxLines, ushort icon,
int msec, bool clickToShow, int id = -1);

/** Update interval to show notification with given @a id. */
void updateInterval(int id, int msec);
Expand Down

0 comments on commit 97b59c6

Please sign in to comment.