1 //////////////////////////////////////////////////////////////////////////////
2 // oxygenexceptiondialog.cpp
3 // -------------------
4 //
5 // SPDX-FileCopyrightText: 2009 Hugo Pereira Da Costa <hugo.pereira@free.fr>
6 //
7 // SPDX-License-Identifier: MIT
8 //////////////////////////////////////////////////////////////////////////////
9 
10 #include "oxygenexceptiondialog.h"
11 #include "oxygendetectwidget.h"
12 #include "config-oxygen.h"
13 
14 #if OXYGEN_HAVE_X11
15 #include <QX11Info>
16 #endif
17 
18 namespace Oxygen
19 {
20 
21     //___________________________________________
ExceptionDialog(QWidget * parent)22     ExceptionDialog::ExceptionDialog( QWidget* parent ):
23         QDialog( parent )
24     {
25 
26         m_ui.setupUi( this );
27 
28         connect( m_ui.buttonBox->button( QDialogButtonBox::Cancel ), SIGNAL(clicked()), this, SLOT(close()) );
29 
30         // store checkboxes from ui into list
31         m_checkboxes.insert( BorderSize, m_ui.borderSizeCheckBox );
32 
33         // detect window properties
34         connect( m_ui.detectDialogButton, SIGNAL(clicked()), SLOT(selectWindowProperties()) );
35 
36         // connections
37         connect( m_ui.exceptionType, SIGNAL(currentIndexChanged(int)), SLOT(updateChanged()) );
38         connect( m_ui.exceptionEditor, SIGNAL(textChanged(QString)), SLOT(updateChanged()) );
39         connect( m_ui.borderSizeComboBox, SIGNAL(currentIndexChanged(int)), SLOT(updateChanged()) );
40 
41         for( CheckBoxMap::iterator iter = m_checkboxes.begin(); iter != m_checkboxes.end(); ++iter )
42         { connect( iter.value(), SIGNAL(clicked()), SLOT(updateChanged()) ); }
43 
44         connect( m_ui.hideTitleBar, SIGNAL(clicked()), SLOT(updateChanged()) );
45 
46         // hide detection dialog on non X11 platforms
47         #if OXYGEN_HAVE_X11
48         if( !QX11Info::isPlatformX11() ) m_ui.detectDialogButton->hide();
49         #else
50         m_ui.detectDialogButton->hide();
51         #endif
52     }
53 
54     //___________________________________________
setException(InternalSettingsPtr exception)55     void ExceptionDialog::setException( InternalSettingsPtr exception )
56     {
57 
58         // store exception internally
59         m_exception = exception;
60 
61         // type
62         m_ui.exceptionType->setCurrentIndex(m_exception->exceptionType() );
63         m_ui.exceptionEditor->setText( m_exception->exceptionPattern() );
64         m_ui.borderSizeComboBox->setCurrentIndex( m_exception->borderSize() );
65         m_ui.hideTitleBar->setChecked( m_exception->hideTitleBar() );
66 
67         // mask
68         for( CheckBoxMap::iterator iter = m_checkboxes.begin(); iter != m_checkboxes.end(); ++iter )
69         { iter.value()->setChecked( m_exception->mask() & iter.key() ); }
70 
71         setChanged( false );
72 
73     }
74 
75     //___________________________________________
save(void)76     void ExceptionDialog::save( void )
77     {
78         m_exception->setExceptionType( m_ui.exceptionType->currentIndex() );
79         m_exception->setExceptionPattern( m_ui.exceptionEditor->text() );
80         m_exception->setBorderSize( m_ui.borderSizeComboBox->currentIndex() );
81         m_exception->setHideTitleBar( m_ui.hideTitleBar->isChecked() );
82 
83         // mask
84         unsigned int mask = None;
85         for( CheckBoxMap::iterator iter = m_checkboxes.begin(); iter != m_checkboxes.end(); ++iter )
86         { if( iter.value()->isChecked() ) mask |= iter.key(); }
87 
88         m_exception->setMask( mask );
89 
90         setChanged( false );
91 
92     }
93 
94     //___________________________________________
updateChanged(void)95     void ExceptionDialog::updateChanged( void )
96     {
97         bool modified( false );
98         if( m_exception->exceptionType() != m_ui.exceptionType->currentIndex() ) modified = true;
99         else if( m_exception->exceptionPattern() != m_ui.exceptionEditor->text() ) modified = true;
100         else if( m_exception->borderSize() != m_ui.borderSizeComboBox->currentIndex() ) modified = true;
101         else if( m_exception->hideTitleBar() != m_ui.hideTitleBar->isChecked() ) modified = true;
102         else
103         {
104             // check mask
105             for( CheckBoxMap::iterator iter = m_checkboxes.begin(); iter != m_checkboxes.end(); ++iter )
106             {
107                 if( iter.value()->isChecked() != (bool)( m_exception->mask() & iter.key() ) )
108                 {
109                     modified = true;
110                     break;
111                 }
112             }
113         }
114 
115         setChanged( modified );
116 
117     }
118 
119     //___________________________________________
selectWindowProperties(void)120     void ExceptionDialog::selectWindowProperties( void )
121     {
122 
123         // create widget
124         if( !m_detectDialog )
125         {
126             m_detectDialog = new DetectDialog( this );
127             connect( m_detectDialog, SIGNAL(detectionDone(bool)), SLOT(readWindowProperties(bool)) );
128         }
129 
130         m_detectDialog->detect(0);
131 
132     }
133 
134     //___________________________________________
readWindowProperties(bool valid)135     void ExceptionDialog::readWindowProperties( bool valid )
136     {
137         Q_CHECK_PTR( m_detectDialog );
138         if( valid )
139         {
140 
141             // type
142             m_ui.exceptionType->setCurrentIndex( m_detectDialog->exceptionType() );
143 
144             // window info
145             const KWindowInfo& info( m_detectDialog->windowInfo() );
146 
147             switch( m_detectDialog->exceptionType() )
148             {
149 
150                 default:
151                 case InternalSettings::ExceptionWindowClassName:
152                 m_ui.exceptionEditor->setText( QString::fromUtf8( info.windowClassClass() ) );
153                 break;
154 
155                 case InternalSettings::ExceptionWindowTitle:
156                 m_ui.exceptionEditor->setText( info.name() );
157                 break;
158 
159             }
160 
161         }
162 
163         delete m_detectDialog;
164         m_detectDialog = 0;
165 
166     }
167 
168 }
169