Changeset View
Changeset View
Standalone View
Standalone View
kcms/componentchooser/componentchooserterminal.cpp
Show All 28 Lines | |||||
29 | #include <kurlrequester.h> | 29 | #include <kurlrequester.h> | ||
30 | #include <kconfiggroup.h> | 30 | #include <kconfiggroup.h> | ||
31 | #include <KLocalizedString> | 31 | #include <KLocalizedString> | ||
32 | #include <KServiceTypeTrader> | 32 | #include <KServiceTypeTrader> | ||
33 | 33 | | |||
34 | #include <QUrl> | 34 | #include <QUrl> | ||
35 | 35 | | |||
36 | CfgTerminalEmulator::CfgTerminalEmulator(QWidget *parent) | 36 | CfgTerminalEmulator::CfgTerminalEmulator(QWidget *parent) | ||
37 | : QWidget(parent), Ui::TerminalEmulatorConfig_UI(), CfgPlugin() | 37 | : QComboBox(parent), CfgPlugin() | ||
38 | { | 38 | { | ||
39 | setupUi(this); | 39 | connect(this, static_cast<void(QComboBox::*)(int)>(&QComboBox::activated), this, &CfgTerminalEmulator::selectTerminalEmulator); | ||
40 | connect(terminalCombo, static_cast<void(QComboBox::*)(int)>(&QComboBox::activated), this, &CfgTerminalEmulator::selectTerminalEmulator); | | |||
41 | } | 40 | } | ||
42 | 41 | | |||
43 | CfgTerminalEmulator::~CfgTerminalEmulator() { | 42 | CfgTerminalEmulator::~CfgTerminalEmulator() { | ||
44 | } | 43 | } | ||
45 | 44 | | |||
46 | void CfgTerminalEmulator::selectTerminalEmulator(int index) | 45 | void CfgTerminalEmulator::selectTerminalEmulator(int index) | ||
47 | { | 46 | { | ||
48 | if (index == terminalCombo->count() - 1) { | 47 | if (index == count() - 1) { | ||
49 | selectTerminalApp(); | 48 | selectTerminalApp(); | ||
50 | } else { | 49 | } else { | ||
51 | emit changed(m_currentIndex != index); | 50 | emit changed(m_currentIndex != index); | ||
52 | } | 51 | } | ||
53 | } | 52 | } | ||
54 | 53 | | |||
55 | void CfgTerminalEmulator::defaults() | 54 | void CfgTerminalEmulator::defaults() | ||
56 | { | 55 | { | ||
57 | if (m_konsoleIndex != -1) { | 56 | if (m_konsoleIndex != -1) { | ||
58 | terminalCombo->setCurrentIndex(m_konsoleIndex); | 57 | setCurrentIndex(m_konsoleIndex); | ||
59 | } | 58 | } | ||
60 | } | 59 | } | ||
61 | 60 | | |||
62 | bool CfgTerminalEmulator::isDefaults() const | 61 | bool CfgTerminalEmulator::isDefaults() const | ||
63 | { | 62 | { | ||
64 | return m_konsoleIndex == -1 || m_konsoleIndex == terminalCombo->currentIndex(); | 63 | return m_konsoleIndex == -1 || m_konsoleIndex == currentIndex(); | ||
65 | } | 64 | } | ||
66 | 65 | | |||
66 | bool CfgTerminalEmulator::hasChanged() const | ||||
67 | { | ||||
68 | return m_currentIndex != -1 && m_currentIndex != currentIndex(); | ||||
69 | } | ||||
67 | void CfgTerminalEmulator::load(KConfig *) | 70 | void CfgTerminalEmulator::load(KConfig *) | ||
68 | { | 71 | { | ||
69 | TerminalSettings settings; | 72 | TerminalSettings settings; | ||
70 | const QString terminal = settings.terminalApplication(); | 73 | const QString terminal = settings.terminalApplication(); | ||
71 | 74 | | |||
72 | m_currentIndex = -1; | 75 | m_currentIndex = -1; | ||
73 | terminalCombo->clear(); | 76 | clear(); | ||
74 | 77 | | |||
75 | const auto constraint = QStringLiteral("'TerminalEmulator' in Categories AND (not exist NoDisplay OR NoDisplay == false)"); | 78 | const auto constraint = QStringLiteral("'TerminalEmulator' in Categories AND (not exist NoDisplay OR NoDisplay == false)"); | ||
76 | const auto terminalEmulators = KServiceTypeTrader::self()->query(QStringLiteral("Application"), constraint); | 79 | const auto terminalEmulators = KServiceTypeTrader::self()->query(QStringLiteral("Application"), constraint); | ||
77 | for (const auto &service : terminalEmulators) { | 80 | for (const auto &service : terminalEmulators) { | ||
78 | terminalCombo->addItem(QIcon::fromTheme(service->icon()), service->name(), service->exec()); | 81 | addItem(QIcon::fromTheme(service->icon()), service->name(), service->exec()); | ||
79 | 82 | | |||
80 | if (!terminal.isEmpty() && service->exec() == terminal) { | 83 | if (!terminal.isEmpty() && service->exec() == terminal) { | ||
81 | terminalCombo->setCurrentIndex(terminalCombo->count() - 1); | 84 | setCurrentIndex(count() - 1); | ||
82 | m_currentIndex = terminalCombo->count() - 1; | 85 | m_currentIndex = count() - 1; | ||
83 | } | 86 | } | ||
84 | if (service->exec() == QStringLiteral("konsole")) { | 87 | if (service->exec() == QStringLiteral("konsole")) { | ||
85 | m_konsoleIndex = terminalCombo->count() - 1; | 88 | m_konsoleIndex = count() - 1; | ||
86 | } | 89 | } | ||
87 | } | 90 | } | ||
88 | 91 | | |||
89 | if (!terminal.isEmpty() && m_currentIndex == -1) { | 92 | if (!terminal.isEmpty() && m_currentIndex == -1) { | ||
90 | // we have a terminal specified by the user | 93 | // we have a terminal specified by the user | ||
91 | terminalCombo->addItem(QIcon::fromTheme(QStringLiteral("application-x-shellscript")), terminal, terminal); | 94 | addItem(QIcon::fromTheme(QStringLiteral("application-x-shellscript")), terminal, terminal); | ||
92 | terminalCombo->setCurrentIndex(terminalCombo->count() - 1); | 95 | setCurrentIndex(count() - 1); | ||
93 | m_currentIndex = terminalCombo->count() - 1; | 96 | m_currentIndex = count() - 1; | ||
94 | } | 97 | } | ||
95 | 98 | | |||
96 | // add a other option to add a new terminal emulator with KOpenWithDialog | 99 | // add a other option to add a new terminal emulator with KOpenWithDialog | ||
97 | terminalCombo->addItem(QIcon::fromTheme(QStringLiteral("application-x-shellscript")), i18n("Other..."), QStringLiteral()); | 100 | addItem(QIcon::fromTheme(QStringLiteral("application-x-shellscript")), i18n("Other..."), QStringLiteral()); | ||
98 | 101 | | |||
99 | emit changed(false); | 102 | emit changed(false); | ||
100 | } | 103 | } | ||
101 | 104 | | |||
102 | void CfgTerminalEmulator::save(KConfig *) | 105 | void CfgTerminalEmulator::save(KConfig *) | ||
103 | { | 106 | { | ||
104 | const QString terminal = terminalCombo->currentData().toString(); | 107 | const QString terminal = currentData().toString(); | ||
105 | m_currentIndex = terminalCombo->currentIndex(); | 108 | m_currentIndex = currentIndex(); | ||
106 | 109 | | |||
107 | TerminalSettings settings; | 110 | TerminalSettings settings; | ||
108 | settings.setTerminalApplication(terminal); | 111 | settings.setTerminalApplication(terminal); | ||
109 | settings.save(); | 112 | settings.save(); | ||
110 | 113 | | |||
114 | m_currentIndex = currentIndex(); | ||||
115 | | ||||
111 | QDBusMessage message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.klauncher5"), | 116 | QDBusMessage message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.klauncher5"), | ||
112 | QStringLiteral("/KLauncher"), | 117 | QStringLiteral("/KLauncher"), | ||
113 | QStringLiteral("org.kde.KLauncher"), | 118 | QStringLiteral("org.kde.KLauncher"), | ||
114 | QStringLiteral("reparseConfiguration")); | 119 | QStringLiteral("reparseConfiguration")); | ||
115 | QDBusConnection::sessionBus().send(message); | 120 | QDBusConnection::sessionBus().send(message); | ||
116 | emit changed(false); | 121 | emit changed(false); | ||
117 | } | 122 | } | ||
118 | 123 | | |||
119 | void CfgTerminalEmulator::selectTerminalApp() | 124 | void CfgTerminalEmulator::selectTerminalApp() | ||
120 | { | 125 | { | ||
121 | QList<QUrl> urlList; | 126 | QList<QUrl> urlList; | ||
122 | KOpenWithDialog dlg(urlList, i18n("Select preferred terminal application:"), QString(), this); | 127 | KOpenWithDialog dlg(urlList, i18n("Select preferred terminal application:"), QString(), this); | ||
123 | // hide "Run in &terminal" here, we don't need it for a Terminal Application | 128 | // hide "Run in &terminal" here, we don't need it for a Terminal Application | ||
124 | dlg.hideRunInTerminal(); | 129 | dlg.hideRunInTerminal(); | ||
125 | dlg.setSaveNewApplications(true); | 130 | dlg.setSaveNewApplications(true); | ||
126 | if (dlg.exec() != QDialog::Accepted) { | 131 | if (dlg.exec() != QDialog::Accepted) { | ||
127 | terminalCombo->setCurrentIndex(m_currentIndex); | 132 | setCurrentIndex(m_currentIndex); | ||
128 | return; | 133 | return; | ||
129 | } | 134 | } | ||
130 | const auto service = dlg.service(); | 135 | const auto service = dlg.service(); | ||
131 | 136 | | |||
132 | // if the selected service is already in the list | 137 | // if the selected service is already in the list | ||
133 | const auto matching = terminalCombo->model()->match(terminalCombo->model()->index(0,0), Qt::DisplayRole, service->exec()); | 138 | const auto matching = model()->match(model()->index(0,0), Qt::DisplayRole, service->exec()); | ||
134 | if (!matching.isEmpty()) { | 139 | if (!matching.isEmpty()) { | ||
135 | const int index = matching.at(0).row(); | 140 | const int index = matching.at(0).row(); | ||
136 | terminalCombo->setCurrentIndex(index); | 141 | setCurrentIndex(index); | ||
137 | changed(index != m_currentIndex); | 142 | changed(index != m_currentIndex); | ||
138 | } else { | 143 | } else { | ||
139 | const QString icon = !service->icon().isEmpty() ? service->icon() : QStringLiteral("application-x-shellscript"); | 144 | const QString icon = !service->icon().isEmpty() ? service->icon() : QStringLiteral("application-x-shellscript"); | ||
140 | terminalCombo->insertItem(terminalCombo->count() -1, QIcon::fromTheme(icon), service->name(), service->exec()); | 145 | insertItem(count() -1, QIcon::fromTheme(icon), service->name(), service->exec()); | ||
141 | terminalCombo->setCurrentIndex(terminalCombo->count() - 2); | 146 | setCurrentIndex(count() - 2); | ||
142 | 147 | | |||
143 | changed(true); | 148 | changed(true); | ||
144 | } | 149 | } | ||
145 | 150 | | |||
146 | } | 151 | } | ||
147 | // vim: sw=4 ts=4 noet | 152 | // vim: sw=4 ts=4 noet |