10#include <config-libkleo.h>
12#include "dnattributeorderconfigwidget.h"
14#include <libkleo/dn.h>
16#include <libkleo_debug.h>
18#include <KLazyLocalizedString>
19#include <KLocalizedString>
44 auto forceAccessibleFocusEventForCurrentItem = [
this]() {
59class Kleo::DNAttributeOrderConfigWidget::DNAttributeOrderConfigWidgetPrivate
62 enum { Right = 0, Left = 1, UUp = 2, Up = 3, Down = 4, DDown = 5 };
64 TreeWidget *availableLV =
nullptr;
65 TreeWidget *currentLV =
nullptr;
66 std::vector<QToolButton *> navTB;
80 , d(new DNAttributeOrderConfigWidgetPrivate)
83 glay->setContentsMargins(0, 0, 0, 0);
84 glay->setColumnStretch(0, 1);
85 glay->setColumnStretch(2, 1);
90 auto availableAttributesLabel =
new QLabel(
i18nc(
"@label:textbox",
"Available attributes:"),
this);
91 glay->addWidget(availableAttributesLabel, row, 0);
92 auto currentAttributesLabel =
new QLabel(
i18nc(
"@label:textbox",
"Current attribute order:"),
this);
93 glay->addWidget(currentAttributesLabel, row, 2);
96 glay->setRowStretch(row, 1);
98 d->availableLV =
new TreeWidget(
this);
99 availableAttributesLabel->setBuddy(d->availableLV);
100 d->availableLV->setAccessibleName(
i18n(
"available attributes"));
101 prepare(d->availableLV);
103 glay->addWidget(d->availableLV, row, 0);
106 d->placeHolderItem->setText(0, QStringLiteral(
"_X_"));
107 d->placeHolderItem->setText(1,
i18n(
"All others"));
110 struct NavButtonInfo {
114 void (DNAttributeOrderConfigWidget::*slot)();
117 static const std::vector<NavButtonInfo> navButtons = {
120 kli18nc(
"@action:button",
"Add"),
121 kli18n(
"Add to current attribute order"),
122 &DNAttributeOrderConfigWidget::slotRightButtonClicked,
127 kli18nc(
"@action:button",
"Remove"),
128 kli18n(
"Remove from current attribute order"),
129 &DNAttributeOrderConfigWidget::slotLeftButtonClicked,
134 kli18nc(
"@action:button",
"Move to Top"),
135 kli18n(
"Move to top"),
136 &DNAttributeOrderConfigWidget::slotDoubleUpButtonClicked,
141 kli18nc(
"@action:button",
"Move Up"),
142 kli18n(
"Move one up"),
143 &DNAttributeOrderConfigWidget::slotUpButtonClicked,
148 kli18nc(
"@action:button",
"Move Down"),
149 kli18n(
"Move one down"),
150 &DNAttributeOrderConfigWidget::slotDownButtonClicked,
155 kli18nc(
"@action:button",
"Move to Bottom"),
156 kli18n(
"Move to bottom"),
157 &DNAttributeOrderConfigWidget::slotDoubleDownButtonClicked,
162 const auto createToolButton = [
this](
const NavButtonInfo &navButton) {
165 tb->setEnabled(
false);
168 tb->setAutoRepeat(navButton.autorepeat);
170 d->navTB.push_back(tb);
177 buttonCol->addWidget(createToolButton(navButtons[DNAttributeOrderConfigWidgetPrivate::Right]));
178 buttonCol->addWidget(createToolButton(navButtons[DNAttributeOrderConfigWidgetPrivate::Left]));
179 buttonCol->addStretch();
181 glay->addLayout(buttonCol, row, 1);
184 d->currentLV =
new TreeWidget(
this);
185 currentAttributesLabel->setBuddy(d->currentLV);
186 d->currentLV->setAccessibleName(
i18n(
"current attribute order"));
187 prepare(d->currentLV);
188 glay->addWidget(d->currentLV, row, 2);
193 buttonCol->addWidget(createToolButton(navButtons[DNAttributeOrderConfigWidgetPrivate::UUp]));
194 buttonCol->addWidget(createToolButton(navButtons[DNAttributeOrderConfigWidgetPrivate::Up]));
195 buttonCol->addWidget(createToolButton(navButtons[DNAttributeOrderConfigWidgetPrivate::Down]));
196 buttonCol->addWidget(createToolButton(navButtons[DNAttributeOrderConfigWidgetPrivate::DDown]));
197 buttonCol->addStretch();
199 glay->addLayout(buttonCol, row, 3);
203 Q_ASSERT(d->navTB.size() == navButtons.size());
204 for (uint i = 0; i < navButtons.size(); ++i) {
213Kleo::DNAttributeOrderConfigWidget::~DNAttributeOrderConfigWidget() =
default;
215void Kleo::DNAttributeOrderConfigWidget::setAttributeOrder(
const QStringList &order)
218 takePlaceHolderItem();
220 d->availableLV->clear();
221 d->currentLV->clear();
225 for (
const auto &entry : order) {
228 takePlaceHolderItem();
229 d->currentLV->insertTopLevelItem(d->currentLV->topLevelItemCount(), d->placeHolderItem);
230 last = d->placeHolderItem;
234 const auto label = DN::attributeNameToLabel(attr);
240 d->currentLV->setCurrentItem(d->currentLV->topLevelItem(0));
245 for (
const auto &attr : all) {
248 item->setText(0, attr);
249 const auto label = DN::attributeNameToLabel(attr);
250 item->setText(1, label);
256 if (!d->placeHolderItem->treeWidget()) {
257 d->availableLV->addTopLevelItem(d->placeHolderItem);
259 d->availableLV->setCurrentItem(d->availableLV->topLevelItem(0));
262void Kleo::DNAttributeOrderConfigWidget::takePlaceHolderItem()
264 if (
QTreeWidget *lv = d->placeHolderItem->treeWidget()) {
269QStringList Kleo::DNAttributeOrderConfigWidget::attributeOrder()
const
278void Kleo::DNAttributeOrderConfigWidget::slotAvailableSelectionChanged()
280 d->navTB[DNAttributeOrderConfigWidgetPrivate::Right]->setEnabled(!d->availableLV->selectedItems().empty());
283void Kleo::DNAttributeOrderConfigWidget::slotCurrentOrderSelectionChanged()
285 const auto selectedItems = d->currentLV->selectedItems();
286 auto selectedItem = selectedItems.empty() ? nullptr : selectedItems.front();
287 enableDisableButtons(selectedItem);
290void Kleo::DNAttributeOrderConfigWidget::enableDisableButtons(
QTreeWidgetItem *item)
292 d->navTB[DNAttributeOrderConfigWidgetPrivate::UUp]->setEnabled(item && d->currentLV->itemAbove(item));
293 d->navTB[DNAttributeOrderConfigWidgetPrivate::Up]->setEnabled(item && d->currentLV->itemAbove(item));
294 d->navTB[DNAttributeOrderConfigWidgetPrivate::Left]->setEnabled(item);
295 d->navTB[DNAttributeOrderConfigWidgetPrivate::Down]->setEnabled(item && d->currentLV->itemBelow(item));
296 d->navTB[DNAttributeOrderConfigWidgetPrivate::DDown]->setEnabled(item && d->currentLV->itemBelow(item));
299void Kleo::DNAttributeOrderConfigWidget::slotUpButtonClicked()
301 if (d->currentLV->selectedItems().isEmpty()) {
305 int itemIndex = d->currentLV->indexOfTopLevelItem(item);
306 if (itemIndex <= 0) {
309 d->currentLV->takeTopLevelItem(itemIndex);
310 d->currentLV->insertTopLevelItem(itemIndex - 1, item);
311 d->currentLV->setCurrentItem(item);
312 enableDisableButtons(item);
316void Kleo::DNAttributeOrderConfigWidget::slotDoubleUpButtonClicked()
318 if (d->currentLV->selectedItems().isEmpty()) {
322 int itemIndex = d->currentLV->indexOfTopLevelItem(item);
323 if (itemIndex == 0) {
326 d->currentLV->takeTopLevelItem(itemIndex);
327 d->currentLV->insertTopLevelItem(0, item);
328 d->currentLV->setCurrentItem(item);
329 enableDisableButtons(item);
333void Kleo::DNAttributeOrderConfigWidget::slotDownButtonClicked()
335 if (d->currentLV->selectedItems().isEmpty()) {
339 int itemIndex = d->currentLV->indexOfTopLevelItem(item);
340 if (itemIndex + 1 >= d->currentLV->topLevelItemCount()) {
343 d->currentLV->takeTopLevelItem(itemIndex);
344 d->currentLV->insertTopLevelItem(itemIndex + 1, item);
345 d->currentLV->setCurrentItem(item);
346 enableDisableButtons(item);
350void Kleo::DNAttributeOrderConfigWidget::slotDoubleDownButtonClicked()
352 if (d->currentLV->selectedItems().isEmpty()) {
356 const int itemIndex = d->currentLV->indexOfTopLevelItem(item);
357 if (itemIndex + 1 >= d->currentLV->topLevelItemCount()) {
360 d->currentLV->takeTopLevelItem(itemIndex);
361 d->currentLV->addTopLevelItem(item);
362 d->currentLV->setCurrentItem(item);
363 enableDisableButtons(item);
367void Kleo::DNAttributeOrderConfigWidget::slotLeftButtonClicked()
369 if (d->currentLV->selectedItems().isEmpty()) {
375 next = d->currentLV->itemAbove(right);
377 d->currentLV->takeTopLevelItem(d->currentLV->indexOfTopLevelItem(right));
378 d->availableLV->addTopLevelItem(right);
380 d->availableLV->setCurrentItem(right);
382 d->currentLV->setCurrentItem(next);
384 enableDisableButtons(next);
388void Kleo::DNAttributeOrderConfigWidget::slotRightButtonClicked()
390 if (d->availableLV->selectedItems().isEmpty()) {
396 next = d->availableLV->itemAbove(left);
398 d->availableLV->takeTopLevelItem(d->availableLV->indexOfTopLevelItem(left));
399 int newRightIndex = d->currentLV->topLevelItemCount();
400 if (!d->currentLV->selectedItems().isEmpty()) {
402 newRightIndex = d->currentLV->indexOfTopLevelItem(right);
404 d->currentLV->insertTopLevelItem(newRightIndex, left);
405 d->currentLV->setCurrentItem(left);
406 enableDisableButtons(left);
407 d->navTB[DNAttributeOrderConfigWidgetPrivate::Right]->setEnabled(next);
409 d->availableLV->setCurrentItem(next);
414void Kleo::DNAttributeOrderConfigWidget::virtual_hook(
int,
void *)
418#include "dnattributeorderconfigwidget.moc"
420#include "moc_dnattributeorderconfigwidget.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
QAction * next(const QObject *recvr, const char *slot, QObject *parent)
virtual void focusInEvent(QFocusEvent *event) override
void addStretch(int stretch)
QIcon fromTheme(const QString &name)
void push_back(parameter_type value)
bool isValid() const const
QString toUpper() const const
QTextStream & left(QTextStream &stream)
QTextStream & right(QTextStream &stream)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void setAllColumnsShowFocus(bool enable)
void setText(int column, const QString &text)