1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
/*
SPDX-FileCopyrightText: 2010 Jan Lepper <krusader@users.sourceforge.net>
SPDX-FileCopyrightText: 2010-2022 Krusader Krew <https://krusader.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "krlayoutfactory.h"
#include "../compat.h"
#include "../krglobal.h"
#include "listpanel.h"
#include "listpanelframe.h"
// QtCore
#include <QDebug>
#include <QFile>
#include <QMetaEnum>
#include <QResource>
#include <QStandardPaths>
// QtWidgets
#include <QHBoxLayout>
#include <QVBoxLayout>
// QtXml
#include <QDomDocument>
#include <KLocalizedString>
#include <KSharedConfig>
#define XMLFILE_VERSION "1.0"
#define MAIN_FILE "layout.xml"
#define MAIN_FILE_RC_PATH ":/" MAIN_FILE
#define EXTRA_FILE_MASK "layouts/*.xml"
#define DEFAULT_LAYOUT "krusader:default"
bool KrLayoutFactory::_parsed = false;
QDomDocument KrLayoutFactory::_mainDoc;
QList<QDomDocument> KrLayoutFactory::_extraDocs;
QString KrLayoutFactory::layoutDescription(const QString &layoutName)
{
if (layoutName == DEFAULT_LAYOUT)
return i18nc("Default layout", "Default");
else if (layoutName == "krusader:compact")
return i18n("Compact");
else if (layoutName == "krusader:classic")
return i18n("Classic");
else
return i18n("Custom layout: \"%1\"", layoutName);
}
bool KrLayoutFactory::parseFiles()
{
if (_parsed)
return true;
_parsed = parseResource(MAIN_FILE_RC_PATH, _mainDoc);
if (!_parsed) {
return false;
}
const QStringList extraFilePaths = QStandardPaths::locateAll(QStandardPaths::AppDataLocation, EXTRA_FILE_MASK);
for (const QString &path : extraFilePaths) {
qWarning() << "extra file: " << path;
QDomDocument doc;
if (parseFile(path, doc))
_extraDocs << doc;
}
return true;
}
bool KrLayoutFactory::parseFile(const QString &path, QDomDocument &doc)
{
bool success = false;
QFile file(path);
if (file.open(QIODevice::ReadOnly))
return parseContent(file.readAll(), path, doc);
else
qWarning() << "can't open" << path;
return success;
}
bool KrLayoutFactory::parseResource(const QString &path, QDomDocument &doc)
{
QFile f(path);
if (f.open( QIODevice::ReadOnly)) {
QTextStream t( &f );
t.setEncoding(QStringConverter::Utf8);
QString data = t.readAll();
return parseContent(data, path, doc);
} else {
qWarning() << "resource does not exist:" << path;
return false;
}
}
bool KrLayoutFactory::parseContent(const QString &content, const QString &fileName, QDomDocument &doc)
{
bool success = false;
QString errorMsg;
if (doc.setContent(content, &errorMsg)) {
QDomElement root = doc.documentElement();
if (root.tagName() == "KrusaderLayout") {
QString version = root.attribute("version");
if (version == XMLFILE_VERSION)
success = true;
else
qWarning() << fileName << "has wrong version" << version << "- required is" << XMLFILE_VERSION;
} else
qWarning() << "root.tagName() != \"KrusaderLayout\"";
} else
qWarning() << "error parsing" << fileName << ":" << errorMsg;
return success;
}
void KrLayoutFactory::getLayoutNames(const QDomDocument &doc, QStringList &names)
{
QDomElement root = doc.documentElement();
for (QDomElement e = root.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) {
if (e.tagName() == "layout") {
QString name(e.attribute("name"));
if (!name.isEmpty() && (name != DEFAULT_LAYOUT))
names << name;
}
}
}
QStringList KrLayoutFactory::layoutNames()
{
QStringList names;
names << DEFAULT_LAYOUT;
if (parseFiles()) {
getLayoutNames(_mainDoc, names);
for (const QDomDocument &doc : std::as_const(_extraDocs))
getLayoutNames(doc, names);
}
return names;
}
QDomElement KrLayoutFactory::findLayout(const QDomDocument &doc, const QString &layoutName)
{
QDomElement root = doc.documentElement();
for (QDomElement e = root.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) {
if (e.tagName() == "layout" && e.attribute("name") == layoutName)
return e;
}
return QDomElement();
}
QLayout *KrLayoutFactory::createLayout(QString layoutName)
{
if (layoutName.isEmpty()) {
KConfigGroup cg(krConfig, "PanelLayout");
layoutName = cg.readEntry("Layout", DEFAULT_LAYOUT);
}
QLayout *layout = nullptr;
if (parseFiles()) {
QDomElement layoutRoot;
layoutRoot = findLayout(_mainDoc, layoutName);
if (layoutRoot.isNull()) {
for (const QDomDocument &doc : std::as_const(_extraDocs)) {
layoutRoot = findLayout(doc, layoutName);
if (!layoutRoot.isNull())
break;
}
}
if (layoutRoot.isNull()) {
qWarning() << "no layout with name" << layoutName << "found";
if (layoutName != DEFAULT_LAYOUT)
return createLayout(DEFAULT_LAYOUT);
} else {
layout = createLayout(layoutRoot, panel);
}
}
if (layout) {
for (auto it = widgets.constBegin(), end = widgets.constEnd(); it != end; ++it) {
qWarning() << "widget" << it.key() << "was not added to the layout";
it.value()->hide();
}
} else
qWarning() << "couldn't load layout" << layoutName;
return layout;
}
QBoxLayout *KrLayoutFactory::createLayout(const QDomElement &e, QWidget *parent)
{
QBoxLayout *l = nullptr;
bool horizontal = false;
if (e.attribute("type") == "horizontal") {
horizontal = true;
l = new QHBoxLayout();
} else if (e.attribute("type") == "vertical")
l = new QVBoxLayout();
else {
qWarning() << "unknown layout type:" << e.attribute("type");
return nullptr;
}
l->setSpacing(0);
l->setContentsMargins(0, 0, 0, 0);
for (QDomElement child = e.firstChildElement(); !child.isNull(); child = child.nextSiblingElement()) {
if (child.tagName() == "layout") {
if (QLayout *childLayout = createLayout(child, parent))
l->addLayout(childLayout);
} else if (child.tagName() == "frame") {
QWidget *frame = createFrame(child, parent);
l->addWidget(frame);
} else if (child.tagName() == "widget") {
if (QWidget *w = widgets.take(child.attribute("name")))
l->addWidget(w);
else
qWarning() << "layout: no such widget:" << child.attribute("name");
} else if (child.tagName() == "hide_widget") {
if (QWidget *w = widgets.take(child.attribute("name")))
w->hide();
else
qWarning() << "layout: no such widget:" << child.attribute("name");
} else if (child.tagName() == "spacer") {
if (horizontal)
l->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Fixed));
else
l->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Fixed, QSizePolicy::Expanding));
}
}
return l;
}
QWidget *KrLayoutFactory::createFrame(const QDomElement &e, QWidget *parent)
{
KConfigGroup cg(krConfig, "PanelLayout");
QString color = cg.readEntry("FrameColor", "default");
if (color == "default")
color = e.attribute("color");
else if (color == "none")
color.clear();
int shadow = -1, shape = -1;
QMetaEnum shadowEnum = QFrame::staticMetaObject.enumerator(QFrame::staticMetaObject.indexOfEnumerator("Shadow"));
QString cfgShadow = cg.readEntry("FrameShadow", "default");
if (cfgShadow != "default")
shadow = shadowEnum.keyToValue(cfgShadow.toLatin1().data());
if (shadow < 0)
shadow = shadowEnum.keyToValue(e.attribute("shadow").toLatin1().data());
QMetaEnum shapeEnum = QFrame::staticMetaObject.enumerator(QFrame::staticMetaObject.indexOfEnumerator("Shape"));
QString cfgShape = cg.readEntry("FrameShape", "default");
if (cfgShape != "default")
shape = shapeEnum.keyToValue(cfgShape.toLatin1().data());
if (shape < 0)
shape = shapeEnum.keyToValue(e.attribute("shape").toLatin1().data());
ListPanelFrame *frame = new ListPanelFrame(parent, color);
frame->setFrameStyle(shape | shadow);
frame->setAcceptDrops(true);
if (QLayout *l = createLayout(e, frame)) {
l->setContentsMargins(frame->frameWidth(), frame->frameWidth(), frame->frameWidth(), frame->frameWidth());
frame->setLayout(l);
}
const QPointer<ListPanel> panelPointer(panel); // 'this' is not a QObject, need to declare field as local variable for lambda
QObject::connect(frame, &ListPanelFrame::dropped, panel, [=](QDropEvent *event) {
// handle drop on inner widgets without own drop handling (e.g. status bar) as drop to current directory
panelPointer->handleDrop(event, frame);
});
if (!color.isEmpty())
QObject::connect(panel, &ListPanel::signalRefreshColors, frame, &ListPanelFrame::refreshColors);
return frame;
}
|