File indexing completed on 2024-04-28 16:32:05

0001 /***************************************************************************
0002     Copyright (C) 2002-2011 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #include "viewstack.h"
0026 #include "detailedlistview.h"
0027 #include "entryiconview.h"
0028 #include "config/tellico_config.h"
0029 
0030 #include <KLocalizedString>
0031 
0032 #include <QIcon>
0033 #include <QHBoxLayout>
0034 #include <QVBoxLayout>
0035 #include <QToolButton>
0036 #include <QStackedWidget>
0037 #include <QSlider>
0038 #include <QButtonGroup>
0039 
0040 using Tellico::ViewStack;
0041 
0042 ViewStack::ViewStack(QWidget* parent_) : QWidget(parent_)
0043     , m_listView(new DetailedListView(this))
0044     , m_iconView(new EntryIconView(this)) {
0045   QBoxLayout* lay = new QVBoxLayout();
0046   lay->setMargin(0);
0047   lay->setSpacing(0);
0048 
0049   QBoxLayout* hlay = new QHBoxLayout();
0050   lay->addLayout(hlay);
0051   hlay->setMargin(0);
0052   hlay->setSpacing(0);
0053 
0054   m_listButton = new QToolButton(this);
0055   m_listButton->setCheckable(true);
0056   m_listButton->setIcon(QIcon::fromTheme(QStringLiteral("view-list-details")));
0057   connect(m_listButton, &QAbstractButton::clicked, this, &ViewStack::showListView);
0058 
0059   m_iconButton = new QToolButton(this);
0060   m_iconButton->setCheckable(true);
0061   m_iconButton->setIcon(QIcon::fromTheme(QStringLiteral("view-list-icons")));
0062   connect(m_iconButton, &QAbstractButton::clicked, this, &ViewStack::showIconView);
0063 
0064   QButtonGroup* bg = new QButtonGroup(this);
0065   bg->addButton(m_listButton);
0066   bg->addButton(m_iconButton);
0067 
0068   hlay->addWidget(m_listButton);
0069   hlay->addWidget(m_iconButton);
0070   hlay->addStretch(10);
0071 
0072   m_decreaseIconSizeButton = new QToolButton(this);
0073   m_decreaseIconSizeButton->setIcon(QIcon::fromTheme(QStringLiteral("zoom-out")));
0074   m_decreaseIconSizeButton->setToolTip(i18n("Decrease the maximum icon size in the icon list view"));
0075   hlay->addWidget(m_decreaseIconSizeButton);
0076   connect(m_decreaseIconSizeButton, &QAbstractButton::clicked, this, &ViewStack::slotDecreaseIconSizeButtonClicked);
0077 
0078   m_iconSizeSlider = new QSlider(Qt::Horizontal, this);
0079   m_iconSizeSlider->setMinimum(MIN_ENTRY_ICON_SIZE);
0080   m_iconSizeSlider->setMaximum(MAX_ENTRY_ICON_SIZE);
0081   m_iconSizeSlider->setSingleStep(SMALL_INCREMENT_ICON_SIZE);
0082   m_iconSizeSlider->setPageStep(LARGE_INCREMENT_ICON_SIZE);
0083   m_iconSizeSlider->setValue(Config::maxIconSize());
0084   m_iconSizeSlider->setTracking(true);
0085   m_iconSizeSlider->setToolTip(i18n("The current maximum icon size is %1.\nMove the slider to change it.", Config::maxIconSize()));
0086   hlay->addWidget(m_iconSizeSlider);
0087   connect(m_iconSizeSlider, &QAbstractSlider::valueChanged, this, &ViewStack::slotIconSizeSliderChanged);
0088 
0089   m_increaseIconSizeButton = new QToolButton(this);
0090   m_increaseIconSizeButton->setIcon(QIcon::fromTheme(QStringLiteral("zoom-in")));
0091   m_increaseIconSizeButton->setToolTip(i18n("Increase the maximum icon size in the icon list view"));
0092   hlay->addWidget(m_increaseIconSizeButton, 0);
0093   connect(m_increaseIconSizeButton, &QAbstractButton::clicked, this, &ViewStack::slotIncreaseIconSizeButtonClicked);
0094 
0095   setIconSizeInterfaceVisible(false);
0096 
0097   m_stack = new QStackedWidget(this);
0098   lay->addWidget(m_stack);
0099   m_stack->addWidget(m_listView);
0100   m_stack->addWidget(m_iconView);
0101 
0102   setLayout(lay);
0103 }
0104 
0105 int ViewStack::currentWidget() const {
0106   if(m_stack->currentWidget() == m_listView) {
0107     return Config::ListView;
0108   } else {
0109     return Config::IconView;
0110   }
0111 }
0112 
0113 void ViewStack::setCurrentWidget(int widget_) {
0114   switch(widget_) {
0115     case Config::ListView:
0116       m_listButton->setChecked(true);
0117       showListView();
0118       break;
0119     case Config::IconView:
0120       m_iconButton->setChecked(true);
0121       showIconView();
0122       break;
0123   }
0124 }
0125 
0126 void ViewStack::showListView() {
0127   setIconSizeInterfaceVisible(false);
0128   m_stack->setCurrentWidget(m_listView);
0129 }
0130 
0131 void ViewStack::showIconView() {
0132   m_stack->setCurrentWidget(m_iconView);
0133   setIconSizeInterfaceVisible(true);
0134 }
0135 
0136 void ViewStack::slotDecreaseIconSizeButtonClicked() {
0137   m_iconSizeSlider->setValue(m_iconSizeSlider->value() - LARGE_INCREMENT_ICON_SIZE);
0138 }
0139 
0140 void ViewStack::slotIncreaseIconSizeButtonClicked() {
0141   m_iconSizeSlider->setValue(m_iconSizeSlider->value() + LARGE_INCREMENT_ICON_SIZE);
0142 }
0143 
0144 void ViewStack::slotIconSizeSliderChanged(int size) {
0145   m_decreaseIconSizeButton->setEnabled(size > MIN_ENTRY_ICON_SIZE);
0146   m_increaseIconSizeButton->setEnabled(size < MAX_ENTRY_ICON_SIZE);
0147   m_iconSizeSlider->setToolTip(i18n("The current maximum icon size is %1.\nMove the slider to change it.", size));
0148   Config::setMaxIconSize(size);
0149   m_iconView->setMaxAllowedIconWidth(size);
0150 }
0151 
0152 void ViewStack::setIconSizeInterfaceVisible(bool visible) {
0153   m_decreaseIconSizeButton->setVisible(visible);
0154   m_increaseIconSizeButton->setVisible(visible);
0155   m_iconSizeSlider->setVisible(visible);
0156 }