File indexing completed on 2024-04-28 16:44:48

0001 /* SPDX-FileCopyrightText: 2008 Michael Jansen <kde@michael-jansen.biz>
0002 
0003    SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #include "window_definition_widget.h"
0007 #include "ui_window_definition_widget.h"
0008 
0009 #include "helper_widgets/window_selector.h"
0010 #include "windows_handler.h"
0011 #include "windows_helper/window_selection_rules.h"
0012 
0013 #include <QDebug>
0014 
0015 WindowDefinitionWidget::WindowDefinitionWidget(KHotKeys::Windowdef_simple *windowdef, QWidget *parent)
0016     : HotkeysWidgetIFace(parent)
0017     , ui(new Ui::WindowDefinitionWidget)
0018     , _windowdef(windowdef)
0019 {
0020     ui->setupUi(this);
0021     connect(ui->window_class_combo, SIGNAL(currentIndexChanged(int)), SLOT(slotWindowClassChanged(int)));
0022     connect(ui->window_title_combo, SIGNAL(currentIndexChanged(int)), SLOT(slotWindowTitleChanged(int)));
0023     connect(ui->window_role_combo, SIGNAL(currentIndexChanged(int)), SLOT(slotWindowRoleChanged(int)));
0024     connect(ui->autodetect, SIGNAL(clicked()), SLOT(slotAutoDetect()));
0025 
0026     // user changes -> isChanged for all others
0027     connect(ui->comment, SIGNAL(textChanged(QString)), _changedSignals, SLOT(map()));
0028     _changedSignals->setMapping(ui->comment, "text");
0029 
0030     connect(ui->window_class, SIGNAL(textChanged(QString)), _changedSignals, SLOT(map()));
0031     _changedSignals->setMapping(ui->window_class, "window_class");
0032 
0033     connect(ui->window_role, SIGNAL(textChanged(QString)), _changedSignals, SLOT(map()));
0034     _changedSignals->setMapping(ui->window_role, "window_role");
0035 
0036     connect(ui->window_title, SIGNAL(textChanged(QString)), _changedSignals, SLOT(map()));
0037     _changedSignals->setMapping(ui->window_title, "window_title");
0038 
0039     connect(ui->type_dialog, SIGNAL(toggled(bool)), _changedSignals, SLOT(map()));
0040     _changedSignals->setMapping(ui->type_dialog, "window_type_dialog");
0041 
0042     connect(ui->type_dock, SIGNAL(toggled(bool)), _changedSignals, SLOT(map()));
0043     _changedSignals->setMapping(ui->type_dock, "window_type_dock");
0044 
0045     connect(ui->type_desktop, SIGNAL(toggled(bool)), _changedSignals, SLOT(map()));
0046     _changedSignals->setMapping(ui->type_desktop, "window_type_desktop");
0047 
0048     connect(ui->type_normal, SIGNAL(toggled(bool)), _changedSignals, SLOT(map()));
0049     _changedSignals->setMapping(ui->type_normal, "window_type_normal");
0050 }
0051 
0052 WindowDefinitionWidget::~WindowDefinitionWidget()
0053 {
0054     delete ui;
0055 }
0056 
0057 void WindowDefinitionWidget::doCopyFromObject()
0058 {
0059     ui->comment->setText(_windowdef->comment());
0060     ui->window_class->setText(_windowdef->wclass());
0061     ui->window_class_combo->setCurrentIndex(_windowdef->wclass_match_type());
0062     ui->window_role->setText(_windowdef->role());
0063     ui->window_role_combo->setCurrentIndex(_windowdef->role_match_type());
0064     ui->window_title->setText(_windowdef->title());
0065     ui->window_title_combo->setCurrentIndex(_windowdef->title_match_type());
0066     ui->type_desktop->setChecked(_windowdef->type_match(KHotKeys::Windowdef_simple::WINDOW_TYPE_DESKTOP));
0067     ui->type_dialog->setChecked(_windowdef->type_match(KHotKeys::Windowdef_simple::WINDOW_TYPE_DIALOG));
0068     ui->type_dock->setChecked(_windowdef->type_match(KHotKeys::Windowdef_simple::WINDOW_TYPE_DOCK));
0069     ui->type_normal->setChecked(_windowdef->type_match(KHotKeys::Windowdef_simple::WINDOW_TYPE_NORMAL));
0070 }
0071 
0072 void WindowDefinitionWidget::doCopyToObject()
0073 {
0074     _windowdef->set_comment(ui->comment->text());
0075     _windowdef->set_wclass(ui->window_class->text());
0076     _windowdef->set_wclass_match_type(static_cast<KHotKeys::Windowdef_simple::substr_type_t>(ui->window_class_combo->currentIndex()));
0077     _windowdef->set_role(ui->window_role->text());
0078     _windowdef->set_role_match_type(static_cast<KHotKeys::Windowdef_simple::substr_type_t>(ui->window_role_combo->currentIndex()));
0079     _windowdef->set_title(ui->window_title->text());
0080     _windowdef->set_title_match_type(static_cast<KHotKeys::Windowdef_simple::substr_type_t>(ui->window_title_combo->currentIndex()));
0081     int types = 0;
0082     if (ui->type_desktop->isChecked())
0083         types |= KHotKeys::Windowdef_simple::WINDOW_TYPE_DESKTOP;
0084     if (ui->type_dialog->isChecked())
0085         types |= KHotKeys::Windowdef_simple::WINDOW_TYPE_DIALOG;
0086     if (ui->type_dock->isChecked())
0087         types |= KHotKeys::Windowdef_simple::WINDOW_TYPE_DOCK;
0088     if (ui->type_normal->isChecked())
0089         types |= KHotKeys::Windowdef_simple::WINDOW_TYPE_NORMAL;
0090     _windowdef->set_window_types(types);
0091 }
0092 
0093 bool WindowDefinitionWidget::isChanged() const
0094 {
0095     // clang-format off
0096     if (_windowdef->comment() != ui->comment->text()
0097         || _windowdef->wclass() != ui->window_class->text()
0098         || _windowdef->wclass_match_type() != ui->window_class_combo->currentIndex()
0099         || _windowdef->role() != ui->window_role->text()
0100         || _windowdef->role_match_type() != ui->window_role_combo->currentIndex()
0101         || _windowdef->title() != ui->window_title->text()
0102         || _windowdef->title_match_type() != ui->window_title_combo->currentIndex())
0103         {
0104         return true;
0105     }
0106     // clang-format on
0107 
0108     int types = 0;
0109     if (ui->type_desktop->isChecked())
0110         types |= KHotKeys::Windowdef_simple::WINDOW_TYPE_DESKTOP;
0111     if (ui->type_dialog->isChecked())
0112         types |= KHotKeys::Windowdef_simple::WINDOW_TYPE_DIALOG;
0113     if (ui->type_dock->isChecked())
0114         types |= KHotKeys::Windowdef_simple::WINDOW_TYPE_DOCK;
0115     if (ui->type_normal->isChecked())
0116         types |= KHotKeys::Windowdef_simple::WINDOW_TYPE_NORMAL;
0117     qDebug() << _windowdef->window_types() << types;
0118     return _windowdef->window_types() != types;
0119 }
0120 
0121 void WindowDefinitionWidget::slotAutoDetect()
0122 {
0123     KHotKeys::WindowSelector *sel = new KHotKeys::WindowSelector(this, SLOT(slotWindowSelected(WId)));
0124     sel->select();
0125 }
0126 
0127 void WindowDefinitionWidget::slotWindowClassChanged(int index)
0128 {
0129     ui->window_class->setEnabled(index != 0);
0130     slotChanged("window_class");
0131 }
0132 
0133 void WindowDefinitionWidget::slotWindowRoleChanged(int index)
0134 {
0135     ui->window_role->setEnabled(index != 0);
0136     slotChanged("window_role");
0137 }
0138 
0139 void WindowDefinitionWidget::slotWindowSelected(WId window)
0140 {
0141     if (window) {
0142         KHotKeys::Window_data data(window);
0143         ui->window_title->setText(data.title);
0144         ui->window_role->setText(data.role);
0145         ui->window_class->setText(data.wclass);
0146         ui->type_normal->setChecked(data.type == NET::Normal);
0147         ui->type_dialog->setChecked(data.type == NET::Dialog);
0148         ui->type_dock->setChecked(data.type == NET::Dock);
0149         ui->type_desktop->setChecked(data.type == NET::Desktop);
0150     }
0151 }
0152 
0153 void WindowDefinitionWidget::slotWindowTitleChanged(int index)
0154 {
0155     ui->window_title->setEnabled(index != 0);
0156     slotChanged("window_title");
0157 }
0158 
0159 #include "moc_window_definition_widget.cpp"