~x2go/x2go/x2goclient_master

« back to all changes in this revision

Viewing changes to cupsprintersettingsdialog.cpp

  • Committer: Mihai Moldovan
  • Date: 2015-03-04 20:15:47 UTC
  • Revision ID: git-v1:b7398771a7abd84ddcff407063edb95dd0a205d3
general: move *.cpp and *.h files to src/ and *.ts files to src/i18n/.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**************************************************************************
2
 
*   Copyright (C) 2005-2015 by Oleksandr Shneyder                         *
3
 
*   o.shneyder@phoca-gmbh.de                                              *
4
 
*                                                                         *
5
 
*   This program is free software; you can redistribute it and/or modify  *
6
 
*   it under the terms of the GNU General Public License as published by  *
7
 
*   the Free Software Foundation; either version 2 of the License, or     *
8
 
*   (at your option) any later version.                                   *
9
 
*   This program is distributed in the hope that it will be useful,       *
10
 
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11
 
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12
 
*   GNU General Public License for more details.                          *
13
 
*                                                                         *
14
 
*   You should have received a copy of the GNU General Public License     *
15
 
*   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
16
 
***************************************************************************/
17
 
 
18
 
#include "cupsprintersettingsdialog.h"
19
 
#include "cupsprint.h"
20
 
#ifndef Q_OS_WIN
21
 
#include <QHeaderView>
22
 
#include <QMessageBox>
23
 
#include <QTimer>
24
 
 
25
 
CUPSPrinterSettingsDialog::CUPSPrinterSettingsDialog (
26
 
    QString prnName,
27
 
    CUPSPrint*  cupsObject,
28
 
    QWidget * parent,
29
 
    Qt::WFlags flags
30
 
) :QDialog ( parent, flags )
31
 
{
32
 
        m_cups=cupsObject;
33
 
        printer=prnName;
34
 
        ui.setupUi ( this );
35
 
        setWindowTitle ( prnName );
36
 
        QList<int> sz;
37
 
        sz<<250<<100;
38
 
        ui.splitter->setSizes ( sz );
39
 
        if ( !m_cups->setCurrentPrinter ( printer ) )
40
 
        {
41
 
                //message here
42
 
                close();
43
 
        }
44
 
        setGeneralTab();
45
 
        setPPDTab();
46
 
        connect ( ( QObject* ) ( ui.buttonBox->button (
47
 
                                     QDialogButtonBox::RestoreDefaults ) ),
48
 
                  SIGNAL ( clicked() ),this,SLOT ( slot_restoreDefaults() ) );
49
 
        connect ( ( QObject* ) ( ui.buttonBox->button (
50
 
                                     QDialogButtonBox::Save ) ),
51
 
                  SIGNAL ( clicked() ),this,SLOT ( slot_saveOptions() ) );
52
 
        connect ( ( QObject* ) ( ui.buttonBox->button (
53
 
                                     QDialogButtonBox::Cancel ) ),
54
 
                  SIGNAL ( clicked() ),this,SLOT ( reject() ) );
55
 
        connect ( ( QObject* ) ( ui.buttonBox->button (
56
 
                                     QDialogButtonBox::Ok ) ),
57
 
                  SIGNAL ( clicked() ),this,SLOT ( slot_ok() ) );
58
 
}
59
 
 
60
 
 
61
 
 
62
 
CUPSPrinterSettingsDialog::~CUPSPrinterSettingsDialog()
63
 
{
64
 
}
65
 
 
66
 
 
67
 
 
68
 
void CUPSPrinterSettingsDialog::setCbBox ( QComboBox* cb,
69
 
        QString optionKeyword )
70
 
{
71
 
        QStringList values;
72
 
        QStringList descriptions;
73
 
        int cur_val=m_cups->getOptionValues ( optionKeyword,
74
 
                                              values,descriptions );
75
 
        if ( cur_val==-1 )
76
 
                cb->setEnabled ( false );
77
 
        else
78
 
        {
79
 
                cb->addItems ( descriptions );
80
 
                cb->setCurrentIndex ( cur_val );
81
 
        }
82
 
}
83
 
 
84
 
void CUPSPrinterSettingsDialog::slot_optionSelected ( QTreeWidgetItem * current,
85
 
        QTreeWidgetItem * )
86
 
{
87
 
        ui.optionsTree->clear();
88
 
        if ( current )
89
 
                if ( current->childCount() ==0 )
90
 
                {
91
 
                        ui.gbOptions->setTitle ( current->text ( 0 ) );
92
 
                        QStringList valueNames, valueTexts;
93
 
                        int selectedValue=m_cups->
94
 
                                          getOptionValues (
95
 
                                              current->text ( 2 ),
96
 
                                              valueNames,valueTexts );
97
 
                        for ( int i=0;i<valueNames.size();++i )
98
 
                        {
99
 
                                QTreeWidgetItem* ritem=new QTreeWidgetItem (
100
 
                                    ui.optionsTree,
101
 
                                    QTreeWidgetItem::Type ) ;
102
 
                                ritem->setText ( 0,valueTexts[i] );
103
 
                                ritem->setText ( 1,valueNames[i] );
104
 
                                if ( i==selectedValue )
105
 
                                        ui.optionsTree->setCurrentItem (
106
 
                                            ritem );
107
 
                        }
108
 
                        return;
109
 
                }
110
 
        ui.gbOptions->setTitle ( tr ( "No option selected" ) );
111
 
}
112
 
 
113
 
void CUPSPrinterSettingsDialog::slot_valueSelected ( QTreeWidgetItem * current,
114
 
        QTreeWidgetItem * )
115
 
{
116
 
        if ( !current )
117
 
                return;
118
 
        QTreeWidgetItem* optionItem=ui.ppdTree->currentItem();
119
 
        QString option=optionItem->text ( 2 );
120
 
        QString newVal=current->text ( 1 );
121
 
        QString prevVal,prevText;
122
 
        m_cups->getOptionValue ( option,prevVal,prevText );
123
 
        if ( prevVal==newVal )
124
 
                return;
125
 
 
126
 
        //we need change current item
127
 
        //do it outside this function
128
 
        setNewValue ( option,newVal ) ;
129
 
        QTimer::singleShot ( 1, this, SLOT ( slot_reloadValues() ) );
130
 
 
131
 
        m_cups->getOptionValue ( option,prevVal,prevText );
132
 
        optionItem->setText ( 1,prevText );
133
 
        optionItem->setText ( 3,prevVal );
134
 
        setGeneralTab();
135
 
}
136
 
 
137
 
 
138
 
void CUPSPrinterSettingsDialog::setGeneralTab()
139
 
{
140
 
        disconnect ( ui.cbPageSize,SIGNAL ( currentIndexChanged ( int ) ),
141
 
                     this,SLOT ( slot_changePSize ( int ) ) );
142
 
        disconnect ( ui.cbMediaType,SIGNAL ( currentIndexChanged ( int ) ),
143
 
                     this,SLOT ( slot_changePType ( int ) ) );
144
 
        disconnect ( ui.cbInputSlot,SIGNAL ( currentIndexChanged ( int ) ),
145
 
                     this,SLOT ( slot_changeISlot ( int ) ) );
146
 
        disconnect ( ui.rbNone,SIGNAL ( clicked ( ) ),
147
 
                     this,SLOT ( slot_changeDuplex() ) );
148
 
        disconnect ( ui.rbShort,SIGNAL ( clicked ( ) ),
149
 
                     this,SLOT ( slot_changeDuplex() ) );
150
 
        disconnect ( ui.rbLong,SIGNAL ( clicked ( ) ),
151
 
                     this,SLOT ( slot_changeDuplex() ) );
152
 
        ui.cbPageSize->clear();
153
 
        ui.cbMediaType->clear();
154
 
        ui.cbInputSlot->clear();
155
 
        setCbBox ( ui.cbPageSize,"PageSize" );
156
 
        setCbBox ( ui.cbMediaType,"MediaType" );
157
 
        setCbBox ( ui.cbInputSlot,"InputSlot" );
158
 
        QString valueName, valueText;
159
 
        ui.rbNone->setChecked ( true );
160
 
        if ( m_cups->getOptionValue ( "Duplex",valueName,valueText ) )
161
 
        {
162
 
                if ( valueName=="DuplexTumble" )
163
 
                        ui.rbShort->setChecked ( true );
164
 
                if ( valueName=="DuplexNoTumble" )
165
 
                        ui.rbLong->setChecked ( true );
166
 
 
167
 
        }
168
 
        else
169
 
                ui.gbDuplex->setEnabled ( false );
170
 
        connect ( ui.cbPageSize,SIGNAL ( currentIndexChanged ( int ) ),
171
 
                  this,SLOT ( slot_changePSize ( int ) ) );
172
 
        connect ( ui.cbMediaType,SIGNAL ( currentIndexChanged ( int ) ),
173
 
                  this,SLOT ( slot_changePType ( int ) ) );
174
 
        connect ( ui.cbInputSlot,SIGNAL ( currentIndexChanged ( int ) ),
175
 
                  this,SLOT ( slot_changeISlot ( int ) ) );
176
 
        connect ( ui.rbNone,SIGNAL ( clicked ( ) ),
177
 
                  this,SLOT ( slot_changeDuplex() ) );
178
 
        connect ( ui.rbShort,SIGNAL ( clicked ( ) ),
179
 
                  this,SLOT ( slot_changeDuplex() ) );
180
 
        connect ( ui.rbLong,SIGNAL ( clicked ( ) ),
181
 
                  this,SLOT ( slot_changeDuplex() ) );
182
 
 
183
 
}
184
 
 
185
 
 
186
 
void CUPSPrinterSettingsDialog::setPPDTab()
187
 
{
188
 
        disconnect ( ui.ppdTree,
189
 
                     SIGNAL ( currentItemChanged ( QTreeWidgetItem*,
190
 
                                                   QTreeWidgetItem* ) ),
191
 
                     this,
192
 
                     SLOT ( slot_optionSelected ( QTreeWidgetItem*,
193
 
                                                  QTreeWidgetItem* ) ) );
194
 
 
195
 
        disconnect ( ui.optionsTree,
196
 
                     SIGNAL ( currentItemChanged ( QTreeWidgetItem*,
197
 
                                                   QTreeWidgetItem* ) ),
198
 
                     this,
199
 
                     SLOT ( slot_valueSelected ( QTreeWidgetItem*,
200
 
                                                 QTreeWidgetItem* ) ) );
201
 
 
202
 
        QString info;
203
 
        bool acceptJobs;
204
 
        QString location;
205
 
        QString model;
206
 
        CUPSPrint::printState state;
207
 
        QString stateReason;
208
 
        QString valueName,valueText;
209
 
        !m_cups->getPrinterInfo ( printer,
210
 
                                  info,acceptJobs,
211
 
                                  location,model,state,stateReason ) ;
212
 
 
213
 
 
214
 
        ui.ppdTree->clear();
215
 
        QTreeWidgetItem* ritem=new QTreeWidgetItem ( ( QTreeWidgetItem* ) 0,
216
 
                QTreeWidgetItem::Type ) ;
217
 
 
218
 
        ritem->setText ( 0,model );
219
 
        ui.ppdTree->addTopLevelItem ( ritem );
220
 
        QStringList grName, grText;
221
 
        m_cups->getOptionGroups ( grName,grText );
222
 
        for ( int i=0;i<grName.size();++i )
223
 
        {
224
 
                QTreeWidgetItem* gritem=new QTreeWidgetItem ( ritem,
225
 
                        QTreeWidgetItem::Type ) ;
226
 
                gritem->setText ( 0,grText[i] );
227
 
                gritem->setText ( 2,grName[i] );
228
 
                QStringList optName, optText;
229
 
                m_cups->getOptionsList ( grName[i],optName,optText );
230
 
                for ( int j=0;j<optName.size();++j )
231
 
                {
232
 
                        QTreeWidgetItem* optitem=new QTreeWidgetItem ( gritem,
233
 
                                QTreeWidgetItem::Type ) ;
234
 
                        optitem->setText ( 0,optText[j] );
235
 
                        optitem->setText ( 2,optName[j] );
236
 
                        m_cups->getOptionValue ( optName[j],valueName,
237
 
                                                 valueText );
238
 
                        optitem->setText ( 1,valueText );
239
 
                        optitem->setText ( 3,valueName );
240
 
                }
241
 
 
242
 
        }
243
 
        ui.ppdTree->expandAll();
244
 
        ui.ppdTree->header()->resizeSections ( QHeaderView::ResizeToContents );
245
 
        slot_optionSelected ( ritem,0l );
246
 
        connect ( ui.ppdTree,
247
 
                  SIGNAL ( currentItemChanged ( QTreeWidgetItem*,
248
 
                                                QTreeWidgetItem* ) ),
249
 
                  this,
250
 
                  SLOT ( slot_optionSelected ( QTreeWidgetItem*,
251
 
                                               QTreeWidgetItem* ) ) );
252
 
 
253
 
        connect ( ui.optionsTree,
254
 
                  SIGNAL ( currentItemChanged ( QTreeWidgetItem*,
255
 
                                                QTreeWidgetItem* ) ),
256
 
                  this,
257
 
                  SLOT ( slot_valueSelected ( QTreeWidgetItem*,
258
 
                                              QTreeWidgetItem* ) ) );
259
 
 
260
 
}
261
 
 
262
 
 
263
 
bool CUPSPrinterSettingsDialog::setNewValue ( const QString& option,
264
 
        const QString& value )
265
 
{
266
 
        QString confVal,confOpt;
267
 
        bool res=m_cups->setValue ( option,value,confOpt,confVal );
268
 
        if ( !res )
269
 
        {
270
 
                QString textMessage=
271
 
                    tr ( "This value is in conflict with other option" );
272
 
                QString txt;
273
 
                m_cups->getOptionText ( confOpt,txt );
274
 
                QString val,valt;
275
 
                m_cups->getOptionValue ( confOpt,val,valt );
276
 
                if ( confOpt.length() >0 &&confVal.length() >0 )
277
 
                {
278
 
                        textMessage+="\n("+txt+" : "+valt+")";
279
 
 
280
 
                }
281
 
                QMessageBox::critical ( this,tr ( "Options conflict" ),
282
 
                                        textMessage );
283
 
        }
284
 
        return res;
285
 
}
286
 
 
287
 
 
288
 
void CUPSPrinterSettingsDialog::slot_reloadValues()
289
 
{
290
 
        if ( ui.ppdTree->currentItem() )
291
 
                slot_optionSelected ( ui.ppdTree->currentItem(),0l );
292
 
        QTreeWidgetItemIterator it ( ui.ppdTree );
293
 
        while ( *it )
294
 
        {
295
 
                if ( ( *it )->childCount() ==0 )
296
 
                {
297
 
                        QString opt= ( *it )->text ( 2 );
298
 
                        QString nval,ntext;
299
 
                        m_cups->getOptionValue ( opt,nval,ntext );
300
 
                        if ( ( *it )->text ( 3 ) != nval )
301
 
                                ( *it )->setText ( 1,ntext );
302
 
                        ( *it )->setText ( 3,nval );
303
 
                }
304
 
                ++it;
305
 
        }
306
 
}
307
 
 
308
 
 
309
 
void CUPSPrinterSettingsDialog::slot_changePSize ( int ind )
310
 
{
311
 
        changeFromCbBox ( "PageSize",ind );
312
 
 
313
 
}
314
 
 
315
 
void CUPSPrinterSettingsDialog::slot_changePType ( int ind )
316
 
{
317
 
        changeFromCbBox ( "MediaType",ind );
318
 
}
319
 
 
320
 
void CUPSPrinterSettingsDialog::slot_changeISlot ( int ind )
321
 
{
322
 
        changeFromCbBox ( "InputSlot",ind );
323
 
}
324
 
 
325
 
void CUPSPrinterSettingsDialog::changeFromCbBox ( const QString& opt, int id )
326
 
{
327
 
        QStringList vals,texts;
328
 
        m_cups->getOptionValues ( opt,vals,texts );
329
 
        if ( vals.size() <id )
330
 
                return;
331
 
        changeGeneralOption ( opt,vals[id] );
332
 
}
333
 
 
334
 
void CUPSPrinterSettingsDialog::slot_changeDuplex()
335
 
{
336
 
        if ( ui.rbShort->isChecked() )
337
 
        {
338
 
                changeGeneralOption ( "Duplex","DuplexTumble" );
339
 
        }
340
 
        if ( ui.rbLong->isChecked() )
341
 
        {
342
 
                changeGeneralOption ( "Duplex","DuplexNoTumble" );
343
 
        }
344
 
        if ( ui.rbNone->isChecked() )
345
 
        {
346
 
                changeGeneralOption ( "Duplex","None" );
347
 
        }
348
 
}
349
 
 
350
 
void CUPSPrinterSettingsDialog::changeGeneralOption ( const QString&  option,
351
 
        const QString& val )
352
 
{
353
 
        if ( !setNewValue ( option,val ) )
354
 
                QTimer::singleShot ( 1, this, SLOT ( setGeneralTab() ) );
355
 
        slot_reloadValues();
356
 
}
357
 
 
358
 
void CUPSPrinterSettingsDialog::slot_restoreDefaults()
359
 
{
360
 
        m_cups->setDefaults();
361
 
        setGeneralTab();
362
 
        slot_reloadValues();
363
 
}
364
 
 
365
 
void CUPSPrinterSettingsDialog::slot_saveOptions()
366
 
{
367
 
        m_cups->saveOptions();
368
 
}
369
 
 
370
 
void CUPSPrinterSettingsDialog::slot_ok()
371
 
{
372
 
        m_cups->saveOptions();
373
 
        accept();
374
 
}
375
 
 
376
 
#endif