Update QTreeView tooltips as they change #3403

This commit is contained in:
Christian Kamm 2016-01-21 11:32:27 +01:00
parent 41f43feecf
commit 10a7128d1a
4 changed files with 114 additions and 0 deletions

View file

@ -84,6 +84,7 @@ set(client_SRCS
proxyauthhandler.cpp
proxyauthdialog.cpp
synclogdialog.cpp
tooltipupdater.cpp
creds/credentialsfactory.cpp
creds/httpcredentialsgui.cpp
creds/shibbolethcredentials.cpp

View file

@ -29,6 +29,7 @@
#include "accountmanager.h"
#include "owncloudsetupwizard.h"
#include "creds/abstractcredentials.h"
#include "tooltipupdater.h"
#include <math.h>
@ -87,6 +88,8 @@ AccountSettings::AccountSettings(AccountState *accountState, QWidget *parent) :
#else
ui->_folderList->setMinimumWidth( 300 );
#endif
new ToolTipUpdater(ui->_folderList);
createAccountToolbox();
connect(AccountManager::instance(), SIGNAL(accountAdded(AccountState*)),
SLOT(slotAccountAdded(AccountState*)));

View file

@ -0,0 +1,59 @@
/*
* Copyright (C) by Christian Kamm <mail@ckamm.de>
*
* This program 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; version 2 of the License.
*
* This program 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.
*/
#include "tooltipupdater.h"
#include <QTreeView>
#include <QHelpEvent>
#include <QToolTip>
#include <QDebug>
using namespace OCC;
ToolTipUpdater::ToolTipUpdater(QTreeView* treeView)
: QObject(treeView)
, _treeView(treeView)
{
connect(_treeView->model(), SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)),
SLOT(dataChanged(QModelIndex,QModelIndex,QVector<int>)));
_treeView->viewport()->installEventFilter(this);
}
bool ToolTipUpdater::eventFilter(QObject* /*obj*/, QEvent* ev)
{
if (ev->type() == QEvent::ToolTip)
{
QHelpEvent *helpEvent = static_cast<QHelpEvent *>(ev);
_toolTipPos = helpEvent->globalPos();
}
return false;
}
void ToolTipUpdater::dataChanged(const QModelIndex& topLeft,
const QModelIndex& bottomRight,
const QVector<int>& roles)
{
if (!QToolTip::isVisible() || !roles.contains(Qt::ToolTipRole) || _toolTipPos.isNull()) {
return;
}
// Was it the item under the cursor that changed?
auto index = _treeView->indexAt(_treeView->mapFromGlobal(QCursor::pos()));
if (topLeft == bottomRight && index != topLeft) {
return;
}
// Update the currently active tooltip
QToolTip::showText(_toolTipPos, _treeView->model()->data(index, Qt::ToolTipRole).toString());
}

51
src/gui/tooltipupdater.h Normal file
View file

@ -0,0 +1,51 @@
/*
* Copyright (C) by Christian Kamm <mail@ckamm.de>
*
* This program 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; version 2 of the License.
*
* This program 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.
*/
#include <QObject>
#include <QPoint>
class QTreeView;
namespace OCC {
/**
* @brief Updates tooltips of items in a QTreeView when they change.
* @ingroup gui
*
* Usually tooltips are not updated as they change. Since we want to
* use tooltips to show rapidly updating progress information, we
* need to make sure that that information is displayed to the user
* as it changes.
*
* To accomplish that, the eventFilter() stores the tooltip's position
* and the dataChanged() slot updates the tooltip if Qt::ToolTipRole
* gets updated while a tooltip is shown.
*/
class ToolTipUpdater : public QObject
{
Q_OBJECT
public:
ToolTipUpdater(QTreeView* treeView);
protected:
bool eventFilter(QObject* obj, QEvent* ev) Q_DECL_OVERRIDE;
private slots:
void dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight, const QVector<int>& roles);
private:
QTreeView* _treeView;
QPoint _toolTipPos;
};
} // namespace OCC