File indexing completed on 2024-05-12 04:19:40

0001 // vim: set tabstop=4 shiftwidth=4 expandtab:
0002 /*
0003 Gwenview: an image viewer
0004 Copyright 2008 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, Cambridge, MA 02110-1301, USA.
0019 
0020 */
0021 // Self
0022 #include "messageviewadapter.h"
0023 
0024 // Qt
0025 #include <QGraphicsProxyWidget>
0026 
0027 // KF
0028 #include <KLocalizedString>
0029 
0030 // Local
0031 #include "gwenview_lib_debug.h"
0032 #include <lib/document/document.h>
0033 #include <lib/ui_messageview.h>
0034 
0035 namespace Gwenview
0036 {
0037 struct MessageViewAdapterPrivate : Ui_MessageView {
0038     Document::Ptr mDocument;
0039 };
0040 
0041 MessageViewAdapter::MessageViewAdapter()
0042     : d(new MessageViewAdapterPrivate)
0043 {
0044     auto widget = new QWidget;
0045     widget->installEventFilter(this);
0046     d->setupUi(widget);
0047     d->mMessageWidget->setCloseButtonVisible(false);
0048     d->mMessageWidget->setWordWrap(true);
0049 
0050     setInfoMessage(i18n("No document selected"));
0051 
0052     widget->setAutoFillBackground(true);
0053     widget->setBackgroundRole(QPalette::Base);
0054     widget->setForegroundRole(QPalette::Text);
0055 
0056     auto proxy = new QGraphicsProxyWidget;
0057     proxy->setWidget(widget);
0058     setWidget(proxy);
0059 }
0060 
0061 MessageViewAdapter::~MessageViewAdapter()
0062 {
0063     delete d;
0064 }
0065 
0066 void MessageViewAdapter::setErrorMessage(const QString &main, const QString &detail)
0067 {
0068     if (main.isEmpty()) {
0069         d->mMessageWidget->hide();
0070         return;
0071     }
0072     d->mMessageWidget->show();
0073     d->mMessageWidget->setMessageType(KMessageWidget::Error);
0074     QString message;
0075     if (detail.isEmpty()) {
0076         message = main;
0077     } else {
0078         message = QStringLiteral("<b>%1</b><br>%2").arg(main, detail);
0079     }
0080     d->mMessageWidget->setText(message);
0081 }
0082 
0083 void MessageViewAdapter::setInfoMessage(const QString &message)
0084 {
0085     if (message.isEmpty()) {
0086         d->mMessageWidget->hide();
0087         return;
0088     }
0089     d->mMessageWidget->show();
0090     d->mMessageWidget->setMessageType(KMessageWidget::Information);
0091     d->mMessageWidget->setText(message);
0092 }
0093 
0094 Document::Ptr MessageViewAdapter::document() const
0095 {
0096     return d->mDocument;
0097 }
0098 
0099 void MessageViewAdapter::setDocument(const Document::Ptr &doc)
0100 {
0101     d->mDocument = doc;
0102 }
0103 
0104 bool MessageViewAdapter::eventFilter(QObject *, QEvent *ev)
0105 {
0106     if (ev->type() == QEvent::KeyPress) {
0107         auto event = static_cast<QKeyEvent *>(ev);
0108         if (event->modifiers() != Qt::NoModifier) {
0109             return false;
0110         }
0111 
0112         switch (event->key()) {
0113         case Qt::Key_Left:
0114         case Qt::Key_Up:
0115             Q_EMIT previousImageRequested();
0116             break;
0117         case Qt::Key_Right:
0118         case Qt::Key_Down:
0119             Q_EMIT nextImageRequested();
0120             break;
0121         default:
0122             break;
0123         }
0124     }
0125     return false;
0126 }
0127 
0128 } // namespace
0129 
0130 #include "moc_messageviewadapter.cpp"