File: multiapplydialog.cpp

package info (click to toggle)
kde-cli-tools 4%3A6.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 7,396 kB
  • sloc: cpp: 4,368; sh: 42; makefile: 7
file content (142 lines) | stat: -rw-r--r-- 4,173 bytes parent folder | download
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
/* This file is part of the KDE project
   SPDX-FileCopyrightText: 2022 Marco Rebhan <me@dblsaiko.net>

   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
*/

#include "multiapplydialog.h"

#include "typeslistitem.h"
#include "typeslistproxyitem.h"
#include "typeslisttreewidget.h"
#include <KLocalizedString>
#include <QDialogButtonBox>
#include <QVBoxLayout>

MultiApplyDialog::MultiApplyDialog(const TypesListItem *source, const QList<TypesListItem *> &sourceItemList, QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle(i18n("Apply To..."));
    resize(400, 500);

    QVBoxLayout *l = new QVBoxLayout(this);
    QDialogButtonBox *buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
    m_mimeTree = new TypesListTreeWidget(this);
    m_mimeTree->setSelectionMode(QAbstractItemView::ExtendedSelection);

    m_mimeTree->setHeaderLabel(i18n("Known Types"));

    l->addWidget(m_mimeTree);
    l->addWidget(buttons);

    connect(m_mimeTree, &QTreeWidget::itemChanged, this, &MultiApplyDialog::onItemChanged);
    connect(buttons, &QDialogButtonBox::accepted, this, &MultiApplyDialog::accept);
    connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);

    load(source, sourceItemList);
}

MultiApplyDialog::~MultiApplyDialog()
{
}

const QList<TypesListItem *> MultiApplyDialog::selected() const
{
    return m_selected;
}

void MultiApplyDialog::onItemChanged(QTreeWidgetItem *qitem, int column)
{
    auto *item = static_cast<TypesListProxyItem *>(qitem);

    if (column != 0) {
        return;
    }

    QTreeWidgetItem *parent = item->parent();

    Qt::CheckState checkState = item->checkState(0);

    switch (checkState) {
    case Qt::Unchecked:
    case Qt::Checked:
        if (parent) {
            if (checkState == Qt::Unchecked) {
                m_selected.removeAll(item->inner());
            } else {
                if (!m_selected.contains(item->inner())) {
                    m_selected.append(item->inner());
                }
            }

            if (parent->childCount() > 1) {
                bool hasOtherState = false;

                for (int i = 0; i < parent->childCount(); i += 1) {
                    auto *child = parent->child(i);

                    if (child != m_source && child->checkState(0) != checkState) {
                        hasOtherState = true;
                        break;
                    }
                }

                if (hasOtherState) {
                    parent->setCheckState(0, Qt::PartiallyChecked);
                } else {
                    parent->setCheckState(0, checkState);
                }
            } else {
                parent->setCheckState(0, checkState);
            }
        }

        for (int i = 0; i < item->childCount(); i += 1) {
            auto *child = item->child(i);

            if (child != m_source) {
                child->setCheckState(0, checkState);
            }
        }

        break;
    case Qt::PartiallyChecked:
        break;
    }
}

void MultiApplyDialog::load(const TypesListItem *source, const QList<TypesListItem *> &sourceItemList)
{
    QMap<QString, TypesListProxyItem *> majorMap;
    m_mimeTree->clear();
    m_itemList.clear();
    m_selected.clear();
    m_source = nullptr;

    for (TypesListItem *type : sourceItemList) {
        const QString mimetype = type->name();
        const int index = mimetype.indexOf(QLatin1Char('/'));
        const QString maj = mimetype.left(index);

        TypesListProxyItem *groupItem = majorMap.value(maj);

        if (!groupItem) {
            groupItem = new TypesListProxyItem(static_cast<TypesListItem *>(type->parent()), m_mimeTree);
            groupItem->setCheckState(0, Qt::Unchecked);
            majorMap.insert(maj, groupItem);
        }

        TypesListProxyItem *item = new TypesListProxyItem(type, groupItem);
        item->setCheckState(0, Qt::Unchecked);

        if (type == source) {
            item->setFlags(Qt::ItemFlags());
            item->setCheckState(0, Qt::Checked);
            m_source = item;
        }

        m_itemList.append(item);
    }
}

#include "moc_multiapplydialog.cpp"