Merge pull request #756 from jpnurmi/add-button

Make the "Add Folder Sync Connection" button act like a button
This commit is contained in:
Roeland Jago Douma 2018-11-02 10:39:00 +01:00 committed by GitHub
commit 40c36a9ed3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 133 additions and 7 deletions

View file

@ -53,6 +53,7 @@ set(client_SRCS
folderman.cpp
folderstatusmodel.cpp
folderstatusdelegate.cpp
folderstatusview.cpp
folderwatcher.cpp
folderwizard.cpp
generalsettings.cpp

View file

@ -117,7 +117,7 @@
</layout>
</item>
<item row="2" column="0">
<widget class="QTreeView" name="_folderList">
<widget class="OCC::FolderStatusView" name="_folderList">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Expanding">
<horstretch>0</horstretch>
@ -275,6 +275,11 @@
<extends>QToolButton</extends>
<header>sslbutton.h</header>
</customwidget>
<customwidget>
<class>OCC::FolderStatusView</class>
<extends>QTreeView</extends>
<header>folderstatusview.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>

View file

@ -16,6 +16,7 @@
#include "folderstatusdelegate.h"
#include "folderstatusmodel.h"
#include "folderstatusview.h"
#include "folderman.h"
#include "accountstate.h"
#include <theme.h>
@ -24,6 +25,7 @@
#include <QFileIconProvider>
#include <QPainter>
#include <QApplication>
#include <QMouseEvent>
inline static QFont makeAliasFont(const QFont &normalFont)
{
@ -110,6 +112,10 @@ int FolderStatusDelegate::rootFolderHeightWithoutErrors(const QFontMetrics &fm,
void FolderStatusDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (index.data(AddButton).toBool()) {
const_cast<QStyleOptionViewItem &>(option).showDecorationSelected = false;
}
QStyledItemDelegate::paint(painter, option, index);
auto textAlign = Qt::AlignLeft;
@ -129,15 +135,15 @@ void FolderStatusDelegate::paint(QPainter *painter, const QStyleOptionViewItem &
int margin = subFm.height() / 4;
if (index.data(AddButton).toBool()) {
QSize hint = sizeHint(option, index);
QStyleOptionButton opt;
static_cast<QStyleOption &>(opt) = option;
opt.state &= ~QStyle::State_Selected;
opt.state |= QStyle::State_Raised;
if (opt.state & QStyle::State_Enabled && opt.state & QStyle::State_MouseOver && index == _pressedIndex) {
opt.state |= QStyle::State_Sunken;
} else {
opt.state |= QStyle::State_Raised;
}
opt.text = addFolderText();
opt.rect.setWidth(qMin(opt.rect.width(), hint.width()));
opt.rect.adjust(0, aliasMargin, 0, -aliasMargin);
opt.rect = QStyle::visualRect(option.direction, option.rect, opt.rect);
opt.rect = addButtonRect(option.rect, option.direction);
painter->save();
painter->setFont(qApp->font("QPushButton"));
QApplication::style()->drawControl(QStyle::CE_PushButton, &opt, painter, option.widget);
@ -352,6 +358,27 @@ void FolderStatusDelegate::paint(QPainter *painter, const QStyleOptionViewItem &
bool FolderStatusDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
const QStyleOptionViewItem &option, const QModelIndex &index)
{
switch (event->type()) {
case QEvent::MouseButtonPress:
case QEvent::MouseMove:
if (const QAbstractItemView *view = qobject_cast<const QAbstractItemView *>(option.widget)) {
QMouseEvent *me = static_cast<QMouseEvent *>(event);
QModelIndex index;
if (me->buttons()) {
index = view->indexAt(me->pos());
}
if (_pressedIndex != index) {
_pressedIndex = index;
view->viewport()->update();
}
}
break;
case QEvent::MouseButtonRelease:
_pressedIndex = QModelIndex();
break;
default:
break;
}
return QStyledItemDelegate::editorEvent(event, model, option, index);
}
@ -375,6 +402,16 @@ QRect FolderStatusDelegate::optionsButtonRect(QRect within, Qt::LayoutDirection
return QStyle::visualRect(direction, within, r);
}
QRect FolderStatusDelegate::addButtonRect(QRect within, Qt::LayoutDirection direction)
{
QFontMetrics fm(qApp->font("QPushButton"));
QStyleOptionButton opt;
opt.text = addFolderText();
QSize size = QApplication::style()->sizeFromContents(QStyle::CT_PushButton, &opt, fm.size(Qt::TextSingleLine, opt.text)).expandedTo(QApplication::globalStrut());
QRect r(QPoint(within.left(), within.top() + within.height() / 2 - size.height() / 2), size);
return QStyle::visualRect(direction, within, r);
}
QRect FolderStatusDelegate::errorsListRect(QRect within)
{
QFont font = QFont();

View file

@ -57,11 +57,13 @@ public:
* return the position of the option button within the item
*/
static QRect optionsButtonRect(QRect within, Qt::LayoutDirection direction);
static QRect addButtonRect(QRect within, Qt::LayoutDirection direction);
static QRect errorsListRect(QRect within);
static int rootFolderHeightWithoutErrors(const QFontMetrics &fm, const QFontMetrics &aliasFm);
private:
static QString addFolderText();
QPersistentModelIndex _pressedIndex;
};
} // namespace OCC

View file

@ -0,0 +1,42 @@
/*
* Copyright (C) 2018 by J-P Nurmi <jpnurmi@gmail.com>
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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 "folderstatusview.h"
#include "folderstatusdelegate.h"
namespace OCC {
FolderStatusView::FolderStatusView(QWidget *parent) : QTreeView(parent)
{
}
QModelIndex FolderStatusView::indexAt(const QPoint &point) const
{
QModelIndex index = QTreeView::indexAt(point);
if (index.data(FolderStatusDelegate::AddButton).toBool() && !visualRect(index).contains(point)) {
return QModelIndex();
}
return index;
}
QRect FolderStatusView::visualRect(const QModelIndex &index) const
{
QRect rect = QTreeView::visualRect(index);
if (index.data(FolderStatusDelegate::AddButton).toBool()) {
return FolderStatusDelegate::addButtonRect(rect, layoutDirection());
}
return rect;
}
} // namespace OCC

View file

@ -0,0 +1,39 @@
/*
* Copyright (C) 2018 by J-P Nurmi <jpnurmi@gmail.com>
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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.
*/
#ifndef FOLDERSTATUSVIEW_H
#define FOLDERSTATUSVIEW_H
#include <QTreeView>
namespace OCC {
/**
* @brief The FolderStatusView class
* @ingroup gui
*/
class FolderStatusView : public QTreeView
{
Q_OBJECT
public:
explicit FolderStatusView(QWidget *parent = nullptr);
QModelIndex indexAt(const QPoint &point) const override;
QRect visualRect(const QModelIndex &index) const override;
};
} // namespace OCC
#endif // FOLDERSTATUSVIEW_H