main.cpp 4.19 KB
Newer Older
1
/*
2
3
4
    SPDX-FileCopyrightText: 2020 Harald Sitter <sitter@kde.org>
    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
5

6
7
#include <QTableView>
#include <QHeaderView>
Laurent Montel's avatar
Laurent Montel committed
8
#include <QVBoxLayout>
9
#include <KCModule>
10
#include <KPluginFactory>
11
12
13
14
15
16
#include <KLocalizedString>
#include <KAboutData>
#include <KTitleWidget>

#include "ksambasharemodel.h"
#include "smbmountmodel.h"
17
18
19
20

class SambaContainer : public KCModule {
Q_OBJECT
public:
21
22
    SambaContainer(QWidget *parent = nullptr, const QVariantList &list = QVariantList());
    ~SambaContainer() override = default;
23
24

private:
25
    QTableView *addTableView(const QString &localizedLabel, QAbstractListModel *model);
26
27
28
29
30
31
};

K_PLUGIN_FACTORY(SambaFactory,
		registerPlugin<SambaContainer>();
)

32
33
34
35
36
37
38
39
40
SambaContainer::SambaContainer(QWidget *parent, const QVariantList &)
    : KCModule(parent)
{
    KAboutData *about = new KAboutData(i18n("kcmsamba"),
                                       i18n("System Information Control Module"),
                                       QString(), QString(), KAboutLicense::GPL,
                                       i18n("(c) 2002 KDE Information Control Module Samba Team"));
    about->addAuthor(i18n("Michael Glauche"), QString(), QStringLiteral("glauche@isa.rwth-aachen.de"));
    about->addAuthor(i18n("Matthias Hoelzer"), QString(), QStringLiteral("hoelzer@kde.org"));
41
42
43
44
	about->addAuthor(i18n("David Faure"), QString(), QStringLiteral("faure@kde.org"));
	about->addAuthor(i18n("Harald Koschinski"), QString(), QStringLiteral("Harald.Koschinski@arcormail.de"));
	about->addAuthor(i18n("Wilco Greven"), QString(), QStringLiteral("greven@kde.org"));
	about->addAuthor(i18n("Alexander Neundorf"), QString(), QStringLiteral("neundorf@kde.org"));
45
    about->addAuthor(i18n("Harald Sitter"), QString(), QStringLiteral("sitter@kde.org"));
46
47
	setAboutData(about);

48
49
50
51
52
    QVBoxLayout *layout = new QVBoxLayout(this);
    Q_ASSERT(this->layout());
    setLayout(layout);

    addTableView(i18n("&Exports"), new KSambaShareModel(this));
53

54
55
56
57
58
    auto importsView = addTableView(i18n("&Imports"), new SmbMountModel(this));
    importsView->horizontalHeader()->setSectionResizeMode(static_cast<int>(SmbMountModel::ColumnRole::Accessible),
                                                          QHeaderView::ResizeToContents);

    setButtons(Help);
59
60
}

61
62
63
64
65
66
67
68
69
70
71
72
73
QTableView *SambaContainer::addTableView(const QString &localizedLabel, QAbstractListModel *model)
{
    QString text = localizedLabel;
    auto title = new KTitleWidget(this);
    // hackily remove ampersand to not break i18n too much in 5.18
    // TODO: replace i18n'd string to one without quick access marker
    title->setText(text.remove("&"));
    title->setLevel(2);
    layout()->addWidget(title);

    auto view = new QTableView(this);
    layout()->addWidget(view);
    view->setModel(model);
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

    // Stretching is a bit awkward because it allows resizing below the sizeHint of
    // the header, effectively cutting off the text. This is made worse by kcmshell
    // which rather unfortunately stacks scrollviews so size hinting is lost along
    // the way allowing the actual window to be (even by default) smaller than
    // what our preferred hint is. To mitigate this problem we manually make
    // the sizeHint's width the minimal size. This is kind of like QSizePolicy::Minimum.
    // https://bugs.kde.org/show_bug.cgi?id=419786
    int maxSectionRequirement = 0;
    for (auto i = 0; i < view->model()->columnCount(); ++i) {
        const int hint = view->horizontalHeader()->sectionSizeHint(i);
        maxSectionRequirement = qMax<int>(maxSectionRequirement, hint);
    }
    view->horizontalHeader()->setMinimumSectionSize(maxSectionRequirement);
    // Combined with the minimum section size this makes sure the default size will
    // be minimal sufficient regardless of parent sizing policies and model content
    // i.e. an empty view will still have fine spacing for header text.
    view->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContentsOnFirstShow);

93
94
95
96
97
98
    view->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    view->horizontalHeader()->reset();
    view->horizontalHeader()->setVisible(true);
    view->verticalHeader()->setVisible(false);

    return view;
99
100
101
}

#include "main.moc"