File indexing completed on 2024-06-16 05:24:45

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2008 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "documentinfoview.hpp"
0010 
0011 //
0012 #include "documentinfotool.hpp"
0013 // KF
0014 #include <KLocalizedString>
0015 #include <KSeparator>
0016 #include <KSqueezedTextLabel>
0017 #include <KIO/Global>
0018 // Qt
0019 #include <QFont>
0020 #include <QLabel>
0021 #include <QVBoxLayout>
0022 #include <QFormLayout>
0023 #include <QLocale>
0024 #include <QMimeType>
0025 #include <QIcon>
0026 
0027 namespace Kasten {
0028 
0029 // size inspired by Dolphin Information sidebar (which uses hardcoded KIconLoader::SizeEnormous)
0030 constexpr int MimeTypeIconSize = 128;
0031 
0032 DocumentInfoView::DocumentInfoView(DocumentInfoTool* tool, QWidget* parent)
0033     : QWidget(parent)
0034     , mTool(tool)
0035 {
0036     auto* baseLayout = new QVBoxLayout(this);
0037 
0038     // icon
0039     mIconLabel = new QLabel(this);
0040 //     int bsize = 66 + 2 * mIconLabel->style()->pixelMetric( QStyle::PM_ButtonMargin );
0041 //     mIconLabel->setFixedSize(bsize, bsize);
0042     mIconLabel->setFixedHeight(MimeTypeIconSize);
0043     mIconLabel->setMinimumWidth(MimeTypeIconSize);
0044     mIconLabel->setAlignment(Qt::AlignHCenter);
0045     baseLayout->addWidget(mIconLabel);
0046 
0047     // file label
0048     mDocumentTitleLabel = new QLabel(this);
0049     QFont font = mDocumentTitleLabel->font();
0050     font.setBold(true);
0051     mDocumentTitleLabel->setFont(font);
0052     mDocumentTitleLabel->setAlignment(Qt::AlignHCenter);
0053     mDocumentTitleLabel->setWordWrap(true);
0054     mDocumentTitleLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
0055     baseLayout->addWidget(mDocumentTitleLabel);
0056 
0057     // separator
0058     auto* separator = new KSeparator(Qt::Horizontal, this);
0059     baseLayout->addWidget(separator);
0060 
0061     // property grid
0062     auto* propertyGrid = new QFormLayout(); // unknown rows
0063 
0064     // type property
0065     mMimeTypeLabel = new QLabel(QString(), this);
0066     mMimeTypeLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
0067     propertyGrid->addRow(i18n("Type:"), mMimeTypeLabel);
0068 
0069     // location property
0070     mLocationLabel = new KSqueezedTextLabel(this);
0071     // force the layout direction to be always LTR
0072     mLocationLabel->setLayoutDirection(Qt::LeftToRight);
0073     // but if we are in RTL mode, align the text to the right
0074     // otherwise the text is on the wrong side of the dialog
0075     if (layoutDirection() == Qt::RightToLeft) {
0076         mLocationLabel->setAlignment(Qt::AlignRight);
0077     }
0078     // TODO: for some reason if building with enable_final flag the compiler sees
0079     // an ambiguous conversion without the explicit Qt::TextInteractionFlags(...)
0080     mLocationLabel->setTextInteractionFlags(Qt::TextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard));
0081     propertyGrid->addRow(i18n("Location:"), mLocationLabel);
0082 
0083     // size property
0084     mSizeLabel = new QLabel(this);
0085     mSizeLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
0086     propertyGrid->addRow(i18n("Size:"), mSizeLabel);
0087 
0088 #if 0
0089     label = new QLabel(i18n("Created/Loaded:"), this);   // TODO: make adjustable depending on document
0090     propertyGrid->addWidget(label, currentPropertyRow, 0, Qt::AlignRight);
0091     currentPropertyRow++;
0092 
0093     label = new QLabel(i18n("Last modified:"), this);
0094     propertyGrid->addWidget(label, currentPropertyRow, 0, Qt::AlignRight);
0095     currentPropertyRow++;
0096 
0097     label = new QLabel(i18n("Last synchronized:"), this);
0098     propertyGrid->addWidget(label, currentPropertyRow, 0, Qt::AlignRight);
0099     currentPropertyRow++;
0100 // last accessed from remote
0101 
0102     KDateTime dt;// = item.time(KFileItem::CreationTime);
0103     if (!dt.isNull()) {
0104         label = new QLabel(i18n("Created:"), this);
0105         propertyGrid->addWidget(label, currentPropertyRow, 0, Qt::AlignRight);
0106 
0107         label = new QLabel(KLocale::global()->formatDateTime(dt), this);
0108         propertyGrid->addWidget(label, currentPropertyRow++, 2);
0109     }
0110 #endif
0111 
0112     baseLayout->addLayout(propertyGrid);
0113     baseLayout->addStretch(10);
0114 
0115     connect(mTool, &DocumentInfoTool::documentTitleChanged,
0116             this, &DocumentInfoView::onDocumentTitleChanged);
0117     connect(mTool, &DocumentInfoTool::documentMimeTypeChanged,
0118             this, &DocumentInfoView::onMimeTypeChanged);
0119     connect(mTool, &DocumentInfoTool::locationChanged,
0120             this, &DocumentInfoView::onLocationChanged);
0121     connect(mTool, &DocumentInfoTool::documentSizeChanged,
0122             this, &DocumentInfoView::onDocumentSizeChanged);
0123     onDocumentTitleChanged(mTool->documentTitle());
0124     onMimeTypeChanged(mTool->mimeType());
0125     onLocationChanged(mTool->location());
0126     onDocumentSizeChanged(mTool->documentSize());
0127 }
0128 
0129 DocumentInfoView::~DocumentInfoView() = default;
0130 
0131 void DocumentInfoView::onDocumentTitleChanged(const QString& documentTitle)
0132 {
0133     mDocumentTitleLabel->setText(documentTitle);
0134 }
0135 
0136 void DocumentInfoView::onMimeTypeChanged(const QMimeType& mimeType)
0137 {
0138     QString mimeTypeComment;
0139     QPixmap mimeTypeIcon;
0140 
0141     if (!mimeType.isValid()) {
0142         mimeTypeComment = QStringLiteral("-");
0143 //         mimeTypeIcon = ?
0144     } else {
0145         mimeTypeComment = mimeType.comment();
0146         mimeTypeIcon = QIcon::fromTheme(mimeType.iconName()).pixmap(MimeTypeIconSize);
0147     }
0148 
0149     mIconLabel->setPixmap(mimeTypeIcon);
0150     mMimeTypeLabel->setText(mimeTypeComment);
0151 }
0152 
0153 void DocumentInfoView::onLocationChanged(const QString& location)
0154 {
0155     const QString entry = location.isEmpty() ?
0156                           i18nc("There is no storage location assigned to yet.", "[None]") :
0157                           location;
0158     mLocationLabel->setText(entry);
0159 }
0160 
0161 void DocumentInfoView::onDocumentSizeChanged(int newSize)
0162 {
0163     const QString size = (newSize != -1) ?
0164                          KIO::convertSize(newSize) + QLatin1String(" (") + QLocale().toString(newSize) + QLatin1Char(')') :
0165                          QStringLiteral("-");
0166     mSizeLabel->setText(size);
0167 }
0168 
0169 }
0170 
0171 #include "moc_documentinfoview.cpp"