File indexing completed on 2024-05-05 05:49:34

0001 /*
0002     SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de>
0003     SPDX-FileCopyrightText: 2015-2018 Andrius Štikonas <andrius@stikonas.eu>
0004 
0005     SPDX-License-Identifier: GPL-3.0-or-later
0006 */
0007 
0008 #include "util/guihelpers.h"
0009 #include "config.h"
0010 
0011 #include <backend/corebackendmanager.h>
0012 
0013 #include <QAction>
0014 #include <QApplication>
0015 #include <QFileInfo>
0016 #include <QIcon>
0017 #include <QHeaderView>
0018 #include <QMenu>
0019 #include <QPainter>
0020 #include <QPixmap>
0021 #include <QProcess>
0022 #include <QRect>
0023 #include <QStandardPaths>
0024 #include <QString>
0025 #include <QTreeWidget>
0026 
0027 #include <KLocalizedString>
0028 #include <KMessageBox>
0029 
0030 #include <unistd.h>
0031 
0032 QIcon createFileSystemColor(FileSystem::Type type, quint32 size)
0033 {
0034     QPixmap pixmap(size, size);
0035     QPainter painter(&pixmap);
0036     painter.setPen(QColor(0, 0, 0));
0037     painter.setBrush(Config::fileSystemColorCode(static_cast<int>(type)));
0038     painter.drawRect(QRect(0, 0, pixmap.width() - 1, pixmap.height() - 1));
0039     painter.end();
0040 
0041     return QIcon(pixmap);
0042 }
0043 
0044 bool loadBackend()
0045 {
0046     if (CoreBackendManager::self()->load(Config::backend()) == false) {
0047         if (CoreBackendManager::self()->load(CoreBackendManager::defaultBackendName())) {
0048             if (!Config::firstRun())
0049                 KMessageBox::error(nullptr,
0050                                xi18nc("@info", "<para>The configured backend plugin \"%1\" could not be loaded.</para>"
0051                                       "<para>Loading the default backend plugin \"%2\" instead.</para>",
0052                                       Config::backend(), CoreBackendManager::defaultBackendName()),
0053                                xi18nc("@title:window", "Error: Could Not Load Backend Plugin"));
0054             Config::setBackend(CoreBackendManager::defaultBackendName());
0055         } else {
0056             KMessageBox::error(nullptr,
0057                                xi18nc("@info", "<para>Neither the configured (\"%1\") nor the default (\"%2\") backend "
0058                                       "plugin could be loaded.</para><para>Please check your installation.</para>",
0059                                       Config::backend(), CoreBackendManager::defaultBackendName()),
0060                                xi18nc("@title:window", "Error: Could Not Load Backend Plugin"));
0061             return false;
0062         }
0063     }
0064 
0065     return true;
0066 }
0067 
0068 Capacity::Unit preferredUnit()
0069 {
0070     return static_cast<Capacity::Unit>(Config::preferredUnit());
0071 }
0072 
0073 void showColumnsContextMenu(const QPoint& p, QTreeWidget& tree)
0074 {
0075     QMenu headerMenu(xi18nc("@title:menu", "Columns"));
0076 
0077     QHeaderView* header = tree.header();
0078 
0079     for (qint32 i = 0; i < tree.model()->columnCount(); i++) {
0080         const int idx = header->logicalIndex(i);
0081         const QString text = tree.model()->headerData(idx, Qt::Horizontal).toString();
0082 
0083         QAction* action = headerMenu.addAction(text);
0084         action->setCheckable(true);
0085         action->setChecked(!header->isSectionHidden(idx));
0086         action->setData(idx);
0087         action->setEnabled(idx > 0);
0088     }
0089 
0090     QAction* action = headerMenu.exec(tree.header()->mapToGlobal(p));
0091 
0092     if (action != nullptr) {
0093         const bool hidden = !action->isChecked();
0094         tree.setColumnHidden(action->data().toInt(), hidden);
0095         if (!hidden)
0096             tree.resizeColumnToContents(action->data().toInt());
0097     }
0098 }
0099 
0100 namespace GuiHelpers
0101 {
0102 
0103 FileSystem::Type defaultFileSystem()
0104 {
0105     return static_cast<FileSystem::Type>(Config::defaultFileSystem());
0106 }
0107 
0108 std::vector<QColor> fileSystemColorCodesFromSettings()
0109 {
0110     std::vector<QColor> cc;
0111     cc.resize(Config::EnumFileSystem::type::COUNT);
0112     for (int i = 0; i < Config::EnumFileSystem::type::COUNT; ++i)
0113     {
0114         cc[i] = Config::fileSystemColorCode(i);
0115     }
0116     return cc;
0117 }
0118 
0119 }