Add a tooltip action widget (actions don't work yet)

This commit is contained in:
David Sansome 2011-09-18 17:43:53 +01:00
parent 2a97a63719
commit 7ec059dc13
5 changed files with 181 additions and 10 deletions

View File

@ -123,6 +123,7 @@ set(SOURCES
globalsearch/globalsearchwidget.cpp
globalsearch/librarysearchprovider.cpp
globalsearch/searchprovider.cpp
globalsearch/tooltipactionwidget.cpp
globalsearch/tooltipresultwidget.cpp
internet/digitallyimportedservice.cpp
@ -363,6 +364,7 @@ set(HEADERS
globalsearch/globalsearchtooltip.h
globalsearch/globalsearchwidget.h
globalsearch/searchprovider.h
globalsearch/tooltipactionwidget.h
globalsearch/tooltipresultwidget.h
internet/digitallyimportedservicebase.h

View File

@ -16,9 +16,11 @@
*/
#include "globalsearchtooltip.h"
#include "tooltipactionwidget.h"
#include "tooltipresultwidget.h"
#include "core/logging.h"
#include <QAction>
#include <QApplication>
#include <QDesktopWidget>
#include <QKeyEvent>
@ -41,6 +43,16 @@ GlobalSearchTooltip::GlobalSearchTooltip(QObject* event_target)
setFocusPolicy(Qt::NoFocus);
setAttribute(Qt::WA_OpaquePaintEvent);
setAttribute(Qt::WA_TranslucentBackground);
add_ = new QAction(tr("Add to playlist"), this);
add_and_play_ = new QAction(tr("Add and play now"), this);
add_and_queue_ = new QAction(tr("Queue track"), this);
replace_ = new QAction(tr("Replace current playlist"), this);
add_->setShortcut(QKeySequence(Qt::Key_Return));
add_and_play_->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Return));
add_and_queue_->setShortcut(QKeySequence(Qt::SHIFT | Qt::Key_Return));
replace_->setShortcut(QKeySequence(Qt::ALT | Qt::Key_Return));
}
void GlobalSearchTooltip::SetResults(const SearchProvider::ResultList& results) {
@ -51,26 +63,29 @@ void GlobalSearchTooltip::SetResults(const SearchProvider::ResultList& results)
// Using a QVBoxLayout here made some weird flickering that I couldn't figure
// out how to fix, so do layout manually.
int y = 9;
int w = 0;
int y = 9;
// Add a widget for each result
foreach (const SearchProvider::Result& result, results) {
QWidget* widget = new TooltipResultWidget(result, this);
widget->move(0, y);
widget->show();
widgets_ << widget;
QSize size_hint(widget->sizeHint());
y += size_hint.height();
w = qMax(w, size_hint.width());
AddWidget(new TooltipResultWidget(result, this), &w, &y);
}
// Add the action widget
QList<QAction*> actions;
actions << add_ << add_and_play_ << add_and_queue_ << replace_;
action_widget_ = new TooltipActionWidget(this);
action_widget_->SetActions(actions);
AddWidget(action_widget_, &w, &y);
// Set the width of each widget
foreach (QWidget* widget, widgets_) {
widget->resize(w, widget->sizeHint().height());
}
// Resize this widget
y += 9;
resize(w, y);
inner_rect_ = rect().adjusted(
@ -81,6 +96,16 @@ void GlobalSearchTooltip::SetResults(const SearchProvider::ResultList& results)
}
}
void GlobalSearchTooltip::AddWidget(QWidget* widget, int* w, int* y) {
widget->move(0, *y);
widget->show();
widgets_ << widget;
QSize size_hint(widget->sizeHint());
*y += size_hint.height();
*w = qMax(*w, size_hint.width());
}
void GlobalSearchTooltip::ShowAt(const QPoint& pointing_to) {
const qreal min_arrow_offset = kBorderRadius + kArrowHeight;
const QRect screen = desktop_->screenGeometry(this);

View File

@ -24,6 +24,8 @@
class QDesktopWidget;
class TooltipActionWidget;
class GlobalSearchTooltip : public QWidget {
Q_OBJECT
@ -44,9 +46,18 @@ protected:
void keyPressEvent(QKeyEvent* e);
void paintEvent(QPaintEvent*);
private:
void AddWidget(QWidget* widget, int* w, int* y);
private:
QDesktopWidget* desktop_;
QAction* add_;
QAction* add_and_play_;
QAction* add_and_queue_;
QAction* replace_;
TooltipActionWidget* action_widget_;
SearchProvider::ResultList results_;
qreal arrow_offset_;
QRect inner_rect_;

View File

@ -0,0 +1,84 @@
/* This file is part of Clementine.
Copyright 2010, David Sansome <me@davidsansome.com>
Clementine is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Clementine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tooltipactionwidget.h"
#include "core/logging.h"
#include <QAction>
#include <QPainter>
const int TooltipActionWidget::kBorder = 16;
const int TooltipActionWidget::kSpacing = 6;
TooltipActionWidget::TooltipActionWidget(QWidget* parent)
: QWidget(parent),
kTextHeight(fontMetrics().height()),
shortcut_width_(0),
description_width_(0)
{
}
void TooltipActionWidget::SetActions(QList<QAction*> actions) {
actions_ = actions;
int h = 3 + kTextHeight * actions.count();
shortcut_width_ = 0;
description_width_ = 0;
foreach (const QAction* action, actions) {
shortcut_width_ =
qMax(shortcut_width_,
fontMetrics().width(action->shortcut().toString(QKeySequence::NativeText)));
description_width_ =
qMax(description_width_, fontMetrics().width(action->text()));
}
size_hint_ = QSize(
kBorder*2 + shortcut_width_ + kSpacing + description_width_, h);
updateGeometry();
update();
}
void TooltipActionWidget::paintEvent(QPaintEvent*) {
int y = 3;
const qreal shortcut_opacity = 0.4;
const qreal description_opacity = 0.7;
QPainter p(this);
p.setPen(palette().color(QPalette::Text));
foreach (const QAction* action, actions_) {
const QRect shortcut_rect(kBorder, y, shortcut_width_, kTextHeight);
const QRect description_rect(shortcut_rect.right() + kSpacing, y,
description_width_, kTextHeight);
p.setOpacity(shortcut_opacity);
p.drawText(shortcut_rect, Qt::AlignRight | Qt::AlignVCenter,
action->shortcut().toString(QKeySequence::NativeText));
p.setOpacity(description_opacity);
p.drawText(description_rect, Qt::AlignVCenter, action->text());
y += kTextHeight;
}
}
void TooltipActionWidget::mousePressEvent(QMouseEvent* e) {
}

View File

@ -0,0 +1,49 @@
/* This file is part of Clementine.
Copyright 2010, David Sansome <me@davidsansome.com>
Clementine is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Clementine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TOOLTIPACTIONWIDGET_H
#define TOOLTIPACTIONWIDGET_H
#include <QWidget>
class TooltipActionWidget : public QWidget {
Q_OBJECT
public:
TooltipActionWidget(QWidget* parent = 0);
static const int kBorder;
static const int kSpacing;
void SetActions(QList<QAction*> actions);
QSize sizeHint() const { return size_hint_; }
protected:
void paintEvent(QPaintEvent*);
void mousePressEvent(QMouseEvent* e);
private:
const int kTextHeight;
QList<QAction*> actions_;
QSize size_hint_;
int shortcut_width_;
int description_width_;
};
#endif // TOOLTIPACTIONWIDGET_H