nextcloud-desktop/src/mirall/statusdialog.cpp

270 lines
8.8 KiB
C++
Raw Normal View History

2011-09-30 19:42:28 +04:00
/*
* Copyright (C) by Klaas Freitag <freitag@kde.org>
*
* 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.
2011-10-12 18:58:54 +04:00
*/
#include <QtCore>
#include <QtGui>
2011-09-26 23:46:26 +04:00
#include "mirall/statusdialog.h"
#include "mirall/folder.h"
#include "mirall/theme.h"
2011-09-26 23:46:26 +04:00
2011-09-26 17:00:12 +04:00
namespace Mirall {
2011-10-12 18:58:54 +04:00
FolderViewDelegate::FolderViewDelegate()
{
}
FolderViewDelegate::~FolderViewDelegate()
{
// TODO Auto-generated destructor stub
}
//alocate each item size in listview.
QSize FolderViewDelegate::sizeHint(const QStyleOptionViewItem & option ,
const QModelIndex & index) const
{
int h = 70; // height 64 + 4px margin top and down
2011-10-12 18:58:54 +04:00
int w = 0;
QString p = qvariant_cast<QString>(index.data(FolderPathRole));
QFont font = QApplication::font();
QFontMetrics fm(font);
w = 8 + fm.boundingRect( p ).width();
return QSize( w, h );
}
void FolderViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QStyledItemDelegate::paint(painter,option,index);
painter->save();
2011-10-20 12:26:40 +04:00
QFont font = QApplication::font();
2011-10-13 18:41:24 +04:00
QFont subFont = QApplication::font();
2011-10-12 18:58:54 +04:00
//font.setPixelSize(font.weight()+);
font.setBold(true);
subFont.setWeight(subFont.weight() - 4);
2011-10-12 18:58:54 +04:00
QFontMetrics fm(font);
QIcon icon = qvariant_cast<QIcon>(index.data(FolderIconRole));
2011-10-13 18:41:24 +04:00
QIcon statusIcon = qvariant_cast<QIcon>(index.data(FolderStatusIcon));
2011-10-12 18:58:54 +04:00
QString aliasText = qvariant_cast<QString>(index.data(FolderNameRole));
QString pathText = qvariant_cast<QString>(index.data(FolderPathRole));
2011-10-13 18:41:24 +04:00
QString statusText = qvariant_cast<QString>(index.data(FolderStatus));
bool syncEnabled = index.data(FolderSyncEnabled).toBool();
QString syncStatus = syncEnabled? tr( "Enabled" ) : tr( "Disabled" );
2011-10-12 18:58:54 +04:00
2011-10-20 12:26:40 +04:00
QSize iconsize(48,48); // = icon.actualSize(option.decorationSize);
2011-10-12 18:58:54 +04:00
QRect headerRect = option.rect;
QRect iconRect = option.rect;
2011-10-12 18:58:54 +04:00
iconRect.setRight(iconsize.width()+30);
iconRect.setTop(iconRect.top()+5);
headerRect.setLeft(iconRect.right());
headerRect.setTop(headerRect.top()+5);
headerRect.setBottom(headerRect.top()+fm.height());
QRect subheaderRect = headerRect;
2011-10-13 18:41:24 +04:00
subheaderRect.setTop(headerRect.bottom());
QFontMetrics fmSub( subFont );
subheaderRect.setBottom(subheaderRect.top()+fmSub.height());
QRect statusRect = subheaderRect;
statusRect.setTop( subheaderRect.bottom() + 5);
2011-10-13 18:41:24 +04:00
statusRect.setBottom( statusRect.top() + fmSub.height());
2011-10-12 18:58:54 +04:00
QRect lastSyncRect = statusRect;
lastSyncRect.setTop( statusRect.bottom());
lastSyncRect.setBottom( lastSyncRect.top() + fmSub.height());
2011-10-12 18:58:54 +04:00
//painter->drawPixmap(QPoint(iconRect.right()/2,iconRect.top()/2),icon.pixmap(iconsize.width(),iconsize.height()));
2011-10-20 12:26:40 +04:00
painter->drawPixmap(QPoint(iconRect.left()+15,iconRect.top()),icon.pixmap(iconsize.width(),iconsize.height()));
2011-10-12 18:58:54 +04:00
2011-10-13 18:41:24 +04:00
painter->drawPixmap(QPoint(option.rect.right() - 4 - 48, option.rect.top() + 8 ), statusIcon.pixmap( 48,48));
2011-10-12 18:58:54 +04:00
painter->setFont(font);
painter->drawText(headerRect, aliasText);
2011-10-13 18:41:24 +04:00
painter->setFont(subFont);
2011-10-12 18:58:54 +04:00
painter->drawText(subheaderRect.left(),subheaderRect.top()+17, pathText);
painter->drawText(lastSyncRect, tr("Last Sync: %1").arg( statusText ));
painter->drawText(statusRect, tr("Sync Status: %1").arg( syncStatus ));
2011-10-12 18:58:54 +04:00
painter->restore();
}
// ====================================================================================
StatusDialog::StatusDialog( Theme *theme, QWidget *parent) :
QDialog(parent),
_theme( theme )
{
2011-10-12 18:58:54 +04:00
setupUi( this );
setWindowTitle( _theme->appName() + QString (" %1" ).arg( _theme->version() ) );
2011-10-12 18:58:54 +04:00
_model = new QStandardItemModel();
FolderViewDelegate *delegate = new FolderViewDelegate();
2011-09-26 23:46:26 +04:00
2011-10-12 18:58:54 +04:00
_folderList->setItemDelegate( delegate );
_folderList->setModel( _model );
2011-10-20 12:26:40 +04:00
_folderList->setMinimumWidth( 300 );
2011-10-12 18:58:54 +04:00
2011-10-20 12:26:40 +04:00
connect(_ButtonClose, SIGNAL(clicked()), this, SLOT(accept()));
2011-10-12 18:58:54 +04:00
connect(_ButtonRemove, SIGNAL(clicked()), this, SLOT(slotRemoveFolder()));
2011-10-20 12:26:40 +04:00
connect(_ButtonFetch, SIGNAL(clicked()), this, SLOT(slotFetchFolder()));
connect(_ButtonPush, SIGNAL(clicked()), this, SLOT(slotPushFolder()));
connect(_ButtonOpenOC, SIGNAL(clicked()), this, SLOT(slotOpenOC()));
connect(_ButtonEnable, SIGNAL(clicked()), this, SLOT(slotEnableFolder()));
connect(_ButtonInfo, SIGNAL(clicked()), this, SLOT(slotInfoFolder()));
2011-10-20 12:26:40 +04:00
_ButtonOpenOC->setEnabled(false);
2011-10-20 12:26:40 +04:00
_ButtonRemove->setEnabled(false);
_ButtonFetch->setEnabled(false);
_ButtonPush->setEnabled(false);
_ButtonEnable->setEnabled(false);
_ButtonInfo->setEnabled(false);
2011-10-20 12:26:40 +04:00
connect(_folderList, SIGNAL(activated(QModelIndex)), SLOT(slotFolderActivated(QModelIndex)));
}
void StatusDialog::slotFolderActivated( const QModelIndex& indx )
{
bool state = indx.isValid();
_ButtonRemove->setEnabled( state );
_ButtonFetch->setEnabled( state );
_ButtonPush->setEnabled( state );
_ButtonEnable->setEnabled( state );
_ButtonInfo->setEnabled( state );
if ( state ) {
bool folderEnabled = _model->data( indx, FolderViewDelegate::FolderSyncEnabled).toBool();
qDebug() << "folder is sync enabled: " << folderEnabled;
if ( folderEnabled ) {
_ButtonEnable->setText( tr( "disable" ) );
} else {
_ButtonEnable->setText( tr( "enable" ) );
}
}
2011-09-26 23:46:26 +04:00
}
2011-10-12 18:58:54 +04:00
void StatusDialog::setFolderList( Folder::Map folders )
2011-09-26 23:46:26 +04:00
{
2011-10-12 18:58:54 +04:00
_model->clear();
foreach( Folder *f, folders ) {
qDebug() << "Folder: " << f;
QStandardItem *item = new QStandardItem();
QIcon icon = _theme->folderIcon( f->backend(), 48 );
item->setData( icon, FolderViewDelegate::FolderIconRole );
item->setData( f->path(), FolderViewDelegate::FolderPathRole );
2011-10-12 18:58:54 +04:00
item->setData( f->alias(), FolderViewDelegate::FolderNameRole );
item->setData( f->syncEnabled(), FolderViewDelegate::FolderSyncEnabled );
qDebug() << "Folder is SyncEnabled: " << f->syncEnabled();
2011-10-13 18:41:24 +04:00
SyncResult res = f->syncResult();
SyncResult::Status status = res.status();
qDebug() << "Status: " << status;
item->setData( _theme->syncStateIcon( status, 64 ), FolderViewDelegate::FolderStatusIcon );
item->setData( _theme->statusHeaderText( status ), FolderViewDelegate::FolderStatus );
item->setData( res.errorString(), FolderViewDelegate::FolderErrorMsg );
2011-10-13 18:41:24 +04:00
2011-10-12 18:58:54 +04:00
_model->appendRow( item );
}
2011-09-26 23:46:26 +04:00
}
void StatusDialog::slotRemoveFolder()
{
2011-10-13 15:55:52 +04:00
QModelIndex selected = _folderList->selectionModel()->currentIndex();
if( selected.isValid() ) {
QString alias = _model->data( selected, FolderViewDelegate::FolderNameRole ).toString();
qDebug() << "Remove Folder alias " << alias;
if( !alias.isEmpty() ) {
emit(removeFolderAlias( alias ));
}
}
}
2011-09-26 17:00:12 +04:00
2011-10-18 12:22:24 +04:00
void StatusDialog::slotFetchFolder()
{
QModelIndex selected = _folderList->selectionModel()->currentIndex();
if( selected.isValid() ) {
QString alias = _model->data( selected, FolderViewDelegate::FolderNameRole ).toString();
qDebug() << "Fetch Folder alias " << alias;
if( !alias.isEmpty() ) {
emit(fetchFolderAlias( alias ));
}
}
}
void StatusDialog::slotPushFolder()
{
QModelIndex selected = _folderList->selectionModel()->currentIndex();
if( selected.isValid() ) {
QString alias = _model->data( selected, FolderViewDelegate::FolderNameRole ).toString();
qDebug() << "Push Folder alias " << alias;
if( !alias.isEmpty() ) {
emit(pushFolderAlias( alias ));
}
}
}
void StatusDialog::slotEnableFolder()
{
QModelIndex selected = _folderList->selectionModel()->currentIndex();
if( selected.isValid() ) {
QString alias = _model->data( selected, FolderViewDelegate::FolderNameRole ).toString();
bool folderEnabled = _model->data( selected, FolderViewDelegate::FolderSyncEnabled).toBool();
qDebug() << "Toggle enabled/disabled Folder alias " << alias << " - current state: " << folderEnabled;
if( !alias.isEmpty() ) {
emit(enableFolderAlias( alias, !folderEnabled ));
}
}
}
void StatusDialog::slotInfoFolder()
{
QModelIndex selected = _folderList->selectionModel()->currentIndex();
if( selected.isValid() ) {
QString alias = _model->data( selected, FolderViewDelegate::FolderNameRole ).toString();
qDebug() << "Info Folder alias " << alias;
if( !alias.isEmpty() ) {
emit(infoFolderAlias( alias ));
}
}
}
void StatusDialog::setOCUrl( const QUrl& url )
{
_OCUrl = url;
if( url.isValid() )
_ButtonOpenOC->setEnabled( true );
}
void StatusDialog::slotOpenOC()
{
if( _OCUrl.isValid() )
QDesktopServices::openUrl( _OCUrl );
}
2011-09-26 17:00:12 +04:00
}
#include "statusdialog.moc"