File indexing completed on 2024-05-05 04:19:17

0001 // vim: set tabstop=4 shiftwidth=4 expandtab:
0002 /*
0003 Gwenview: an image viewer
0004 Copyright 2007 Aurélien Gâteau <agateau@kde.org>
0005 
0006 This program is free software; you can redistribute it and/or
0007 modify it under the terms of the GNU General Public License
0008 as published by the Free Software Foundation; either version 2
0009 of the License, or (at your option) any later version.
0010 
0011 This program is distributed in the hope that it will be useful,
0012 but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 GNU General Public License for more details.
0015 
0016 You should have received a copy of the GNU General Public License
0017 along with this program; if not, write to the Free Software
0018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0019 
0020 */
0021 // Self
0022 #include "imagemetainfodialog.h"
0023 
0024 // Qt
0025 #include <QApplication>
0026 #include <QDialogButtonBox>
0027 #include <QHeaderView>
0028 #include <QPainter>
0029 #include <QPushButton>
0030 #include <QStyledItemDelegate>
0031 #include <QTreeView>
0032 #include <QVBoxLayout>
0033 
0034 // KF
0035 #include <KLocalizedString>
0036 
0037 // STL
0038 #include <memory>
0039 
0040 // Local
0041 #include <lib/preferredimagemetainfomodel.h>
0042 
0043 namespace Gwenview
0044 {
0045 class MetaInfoDelegate : public QStyledItemDelegate
0046 {
0047 public:
0048     MetaInfoDelegate(QObject *parent)
0049         : QStyledItemDelegate(parent)
0050     {
0051     }
0052 
0053 protected:
0054     void paint(QPainter *painter, const QStyleOptionViewItem &_option, const QModelIndex &index) const override
0055     {
0056         QStyleOptionViewItem option = _option;
0057         if (!index.parent().isValid()) {
0058             option.displayAlignment = Qt::AlignCenter | Qt::AlignBottom;
0059             option.font.setBold(true);
0060         }
0061         QStyledItemDelegate::paint(painter, option, index);
0062         if (!index.parent().isValid()) {
0063             painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
0064         }
0065     }
0066 
0067     QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
0068     {
0069         QSize sh = QStyledItemDelegate::sizeHint(option, index);
0070         if (!index.parent().isValid()) {
0071             sh.setHeight(sh.height() * 3 / 2);
0072         }
0073         return sh;
0074     }
0075 };
0076 
0077 /**
0078  * A tree view which is always fully expanded
0079  */
0080 class ExpandedTreeView : public QTreeView
0081 {
0082 public:
0083     explicit ExpandedTreeView(QWidget *parent)
0084         : QTreeView(parent)
0085     {
0086     }
0087 
0088 protected:
0089     void rowsInserted(const QModelIndex &parent, int start, int end) override
0090     {
0091         QTreeView::rowsInserted(parent, start, end);
0092         if (!parent.isValid()) {
0093             for (int row = start; row <= end; ++row) {
0094                 setUpRootIndex(row);
0095             }
0096         }
0097     }
0098 
0099     void reset() override
0100     {
0101         QTreeView::reset();
0102         if (model()) {
0103             for (int row = 0; row < model()->rowCount(); ++row) {
0104                 setUpRootIndex(row);
0105             }
0106         }
0107     }
0108 
0109 private:
0110     void setUpRootIndex(int row)
0111     {
0112         expand(model()->index(row, 0));
0113         setFirstColumnSpanned(row, QModelIndex(), true);
0114     }
0115 };
0116 
0117 struct ImageMetaInfoDialogPrivate {
0118     std::unique_ptr<PreferredImageMetaInfoModel> mModel;
0119     QTreeView *mTreeView;
0120 };
0121 
0122 ImageMetaInfoDialog::ImageMetaInfoDialog(QWidget *parent)
0123     : QDialog(parent)
0124     , d(new ImageMetaInfoDialogPrivate)
0125 {
0126     d->mTreeView = new ExpandedTreeView(this);
0127     d->mTreeView->setRootIsDecorated(false);
0128     d->mTreeView->setIndentation(0);
0129     d->mTreeView->setItemDelegate(new MetaInfoDelegate(d->mTreeView));
0130     setWindowTitle(i18nc("@title:window", "Image Information"));
0131 
0132     setLayout(new QVBoxLayout);
0133     layout()->addWidget(d->mTreeView);
0134     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Close);
0135     layout()->addWidget(buttonBox);
0136     connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0137     connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0138 }
0139 
0140 ImageMetaInfoDialog::~ImageMetaInfoDialog()
0141 {
0142     delete d;
0143 }
0144 
0145 void ImageMetaInfoDialog::setMetaInfo(ImageMetaInfoModel *model, const QStringList &list)
0146 {
0147     if (model) {
0148         d->mModel = std::make_unique<PreferredImageMetaInfoModel>(model, list);
0149         connect(d->mModel.get(), &PreferredImageMetaInfoModel::preferredMetaInfoKeyListChanged, this, &ImageMetaInfoDialog::preferredMetaInfoKeyListChanged);
0150     } else {
0151         d->mModel.reset(nullptr);
0152     }
0153     d->mTreeView->setModel(d->mModel.get());
0154 
0155     const int marginSize = QApplication::style()->pixelMetric(QStyle::PM_LayoutLeftMargin);
0156     d->mTreeView->header()->resizeSection(0, sizeHint().width() / 2 - marginSize * 2);
0157 }
0158 
0159 QSize ImageMetaInfoDialog::sizeHint() const
0160 {
0161     return QSize(400, 300);
0162 }
0163 
0164 } // namespace
0165 
0166 #include "moc_imagemetainfodialog.cpp"