1 /*
2 This file is part of the Okteta Kasten module, made within the KDE community.
3
4 SPDX-FileCopyrightText: 2008 Friedrich W. H. Kossebau <kossebau@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8
9 #include "documentinfoview.hpp"
10
11 //
12 #include "documentinfotool.hpp"
13 // KF
14 #include <KLocalizedString>
15 #include <KIconLoader>
16 #include <KSeparator>
17 #include <KSqueezedTextLabel>
18 #include <KIO/Global>
19 // Qt
20 #include <QFont>
21 #include <QLabel>
22 #include <QLayout>
23 #include <QGridLayout>
24 #include <QLocale>
25 #include <QMimeType>
26
27 namespace Kasten {
28
DocumentInfoView(DocumentInfoTool * tool,QWidget * parent)29 DocumentInfoView::DocumentInfoView(DocumentInfoTool* tool, QWidget* parent)
30 : QWidget(parent)
31 , mTool(tool)
32 {
33 auto* baseLayout = new QVBoxLayout(this);
34 baseLayout->setContentsMargins(0, 0, 0, 0);
35
36 // icon
37 mIconLabel = new QLabel(this);
38 // int bsize = 66 + 2 * mIconLabel->style()->pixelMetric( QStyle::PM_ButtonMargin );
39 // mIconLabel->setFixedSize(bsize, bsize);
40 mIconLabel->setFixedHeight(KIconLoader::SizeEnormous);
41 mIconLabel->setMinimumWidth(KIconLoader::SizeEnormous);
42 mIconLabel->setAlignment(Qt::AlignHCenter);
43 baseLayout->addWidget(mIconLabel);
44
45 // file label
46 mDocumentTitleLabel = new QLabel(this);
47 QFont font = mDocumentTitleLabel->font();
48 font.setBold(true);
49 mDocumentTitleLabel->setFont(font);
50 mDocumentTitleLabel->setAlignment(Qt::AlignHCenter);
51 mDocumentTitleLabel->setWordWrap(true);
52 mDocumentTitleLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
53 baseLayout->addWidget(mDocumentTitleLabel);
54
55 // separator
56 KSeparator* separator = new KSeparator(Qt::Horizontal, this);
57 baseLayout->addWidget(separator);
58
59 // property grid
60 auto* propertyGrid = new QGridLayout(); // unknown rows
61 propertyGrid->setColumnStretch(0, 0);
62 propertyGrid->setColumnStretch(1, 1);
63
64 int currentPropertyRow = 0;
65
66 // type property
67 QLabel* label = new QLabel(i18n("Type:"), this);
68 propertyGrid->addWidget(label, currentPropertyRow, 0, Qt::AlignRight);
69
70 mMimeTypeLabel = new QLabel(QString(), this);
71 propertyGrid->addWidget(mMimeTypeLabel, currentPropertyRow++, 1);
72
73 // location property
74 label = new QLabel(i18n("Location:"), this);
75 propertyGrid->addWidget(label, currentPropertyRow, 0, Qt::AlignRight);
76
77 mLocationLabel = new KSqueezedTextLabel(this);
78 // force the layout direction to be always LTR
79 mLocationLabel->setLayoutDirection(Qt::LeftToRight);
80 // but if we are in RTL mode, align the text to the right
81 // otherwise the text is on the wrong side of the dialog
82 if (layoutDirection() == Qt::RightToLeft) {
83 mLocationLabel->setAlignment(Qt::AlignRight);
84 }
85 // TODO: for some reason if building with enable_final flag the compiler sees
86 // an ambiguous conversion without the explicit Qt::TextInteractionFlags(...)
87 mLocationLabel->setTextInteractionFlags(Qt::TextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard));
88 propertyGrid->addWidget(mLocationLabel, currentPropertyRow++, 1);
89
90 // size property
91 label = new QLabel(i18n("Size:"), this);
92 propertyGrid->addWidget(label, currentPropertyRow, 0, Qt::AlignRight);
93
94 mSizeLabel = new QLabel(this);
95 propertyGrid->addWidget(mSizeLabel, currentPropertyRow++, 1);
96
97 #if 0
98 label = new QLabel(i18n("Created/Loaded:"), this); // TODO: make adjustable depending on document
99 propertyGrid->addWidget(label, currentPropertyRow, 0, Qt::AlignRight);
100 currentPropertyRow++;
101
102 label = new QLabel(i18n("Last modified:"), this);
103 propertyGrid->addWidget(label, currentPropertyRow, 0, Qt::AlignRight);
104 currentPropertyRow++;
105
106 label = new QLabel(i18n("Last synchronized:"), this);
107 propertyGrid->addWidget(label, currentPropertyRow, 0, Qt::AlignRight);
108 currentPropertyRow++;
109 // last accessed from remote
110
111 KDateTime dt;// = item.time(KFileItem::CreationTime);
112 if (!dt.isNull()) {
113 label = new QLabel(i18n("Created:"), this);
114 propertyGrid->addWidget(label, currentPropertyRow, 0, Qt::AlignRight);
115
116 label = new QLabel(KLocale::global()->formatDateTime(dt), this);
117 propertyGrid->addWidget(label, currentPropertyRow++, 2);
118 }
119 #endif
120
121 baseLayout->addLayout(propertyGrid);
122 baseLayout->addStretch(10);
123
124 connect(mTool, &DocumentInfoTool::documentTitleChanged,
125 this, &DocumentInfoView::onDocumentTitleChanged);
126 connect(mTool, &DocumentInfoTool::documentMimeTypeChanged,
127 this, &DocumentInfoView::onMimeTypeChanged);
128 connect(mTool, &DocumentInfoTool::locationChanged,
129 this, &DocumentInfoView::onLocationChanged);
130 connect(mTool, &DocumentInfoTool::documentSizeChanged,
131 this, &DocumentInfoView::onDocumentSizeChanged);
132 onDocumentTitleChanged(mTool->documentTitle());
133 onMimeTypeChanged(mTool->mimeType());
134 onLocationChanged(mTool->location());
135 onDocumentSizeChanged(mTool->documentSize());
136 }
137
138 DocumentInfoView::~DocumentInfoView() = default;
139
onDocumentTitleChanged(const QString & documentTitle)140 void DocumentInfoView::onDocumentTitleChanged(const QString& documentTitle)
141 {
142 mDocumentTitleLabel->setText(documentTitle);
143 }
144
onMimeTypeChanged(const QMimeType & mimeType)145 void DocumentInfoView::onMimeTypeChanged(const QMimeType& mimeType)
146 {
147 QString mimeTypeComment;
148 QPixmap mimeTypeIcon;
149
150 if (!mimeType.isValid()) {
151 mimeTypeComment = QStringLiteral("-");
152 // mimeTypeIcon = ?
153 } else {
154 mimeTypeComment = mimeType.comment();
155 mimeTypeIcon = KIconLoader::global()->loadIcon(mimeType.iconName(), KIconLoader::Desktop, KIconLoader::SizeEnormous);
156 }
157
158 mIconLabel->setPixmap(mimeTypeIcon);
159 mMimeTypeLabel->setText(mimeTypeComment);
160 }
161
onLocationChanged(const QString & location)162 void DocumentInfoView::onLocationChanged(const QString& location)
163 {
164 const QString entry = location.isEmpty() ?
165 i18nc("There is no storage location assigned to yet.", "[None]") :
166 location;
167 mLocationLabel->setText(entry);
168 }
169
onDocumentSizeChanged(int newSize)170 void DocumentInfoView::onDocumentSizeChanged(int newSize)
171 {
172 const QString size = (newSize != -1) ?
173 KIO::convertSize(newSize) + QLatin1String(" (") + QLocale().toString(newSize) + QLatin1Char(')') :
174 QStringLiteral("-");
175 mSizeLabel->setText(size);
176 }
177
178 }
179