~ubuntu-branches/debian/squeeze/djvulibre/squeeze

« back to all changes in this revision

Viewing changes to gui/shared/QT/qd_tbar_mode_piece.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Barak A. Pearlmutter
  • Date: 2004-11-01 16:49:49 UTC
  • Revision ID: james.westby@ubuntu.com-20041101164949-fm4bl2hmkvkseoqw
Tags: upstream-3.5.14
ImportĀ upstreamĀ versionĀ 3.5.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//C-  -*- C++ -*-
 
2
//C- -------------------------------------------------------------------
 
3
//C- DjVuLibre-3.5
 
4
//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
 
5
//C- Copyright (c) 2001  AT&T
 
6
//C-
 
7
//C- This software is subject to, and may be distributed under, the
 
8
//C- GNU General Public License, Version 2. The license should have
 
9
//C- accompanied the software or you may obtain a copy of the license
 
10
//C- from the Free Software Foundation at http://www.fsf.org .
 
11
//C-
 
12
//C- This program is distributed in the hope that it will be useful,
 
13
//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
//C- GNU General Public License for more details.
 
16
//C- 
 
17
//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library
 
18
//C- distributed by Lizardtech Software.  On July 19th 2002, Lizardtech 
 
19
//C- Software authorized us to replace the original DjVu(r) Reference 
 
20
//C- Library notice by the following text (see doc/lizard2002.djvu):
 
21
//C-
 
22
//C-  ------------------------------------------------------------------
 
23
//C- | DjVu (r) Reference Library (v. 3.5)
 
24
//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
 
25
//C- | The DjVu Reference Library is protected by U.S. Pat. No.
 
26
//C- | 6,058,214 and patents pending.
 
27
//C- |
 
28
//C- | This software is subject to, and may be distributed under, the
 
29
//C- | GNU General Public License, Version 2. The license should have
 
30
//C- | accompanied the software or you may obtain a copy of the license
 
31
//C- | from the Free Software Foundation at http://www.fsf.org .
 
32
//C- |
 
33
//C- | The computer code originally released by LizardTech under this
 
34
//C- | license and unmodified by other parties is deemed "the LIZARDTECH
 
35
//C- | ORIGINAL CODE."  Subject to any third party intellectual property
 
36
//C- | claims, LizardTech grants recipient a worldwide, royalty-free, 
 
37
//C- | non-exclusive license to make, use, sell, or otherwise dispose of 
 
38
//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the 
 
39
//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU 
 
40
//C- | General Public License.   This grant only confers the right to 
 
41
//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to 
 
42
//C- | the extent such infringement is reasonably necessary to enable 
 
43
//C- | recipient to make, have made, practice, sell, or otherwise dispose 
 
44
//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to 
 
45
//C- | any greater extent that may be necessary to utilize further 
 
46
//C- | modifications or combinations.
 
47
//C- |
 
48
//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
 
49
//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 
50
//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
 
51
//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 
52
//C- +------------------------------------------------------------------
 
53
// 
 
54
// $Id: qd_tbar_mode_piece.cpp,v 1.9 2003/11/07 22:08:18 leonb Exp $
 
55
// $Name: release_3_5_14 $
 
56
 
 
57
#ifdef HAVE_CONFIG_H
 
58
# include "config.h"
 
59
#endif
 
60
#if NEED_GNUG_PRAGMAS
 
61
# pragma implementation
 
62
#endif
 
63
 
 
64
#include "qd_tbar_mode_piece.h"
 
65
#include "debug.h"
 
66
#include "qlib.h"
 
67
#include "qd_base.h"
 
68
#include "qd_toolbutt.h"
 
69
#include "djvu_base_res.h"
 
70
#include "cin_data.h"
 
71
 
 
72
#include <qcombobox.h>
 
73
#include <qtooltip.h>
 
74
#include <qlabel.h>
 
75
#include <qvalidator.h>
 
76
#include <qcheckbox.h>
 
77
 
 
78
#include <ctype.h>
 
79
#include <stdlib.h>
 
80
#include <stdio.h>
 
81
 
 
82
//  #define ONE2ONE_STR "1 : 1"
 
83
//  #define STRETCH_STR "Stretch"
 
84
//  #define FIT_WIDTH_STR       "Fit Width"
 
85
//  #define FIT_PAGE_STR        "Fit Page"
 
86
 
 
87
class QDZoomValidator : public QValidator
 
88
{
 
89
public:
 
90
   virtual void         fixup(QString &);
 
91
#ifdef QT1
 
92
   virtual State        validate(QString &, int &);
 
93
#else
 
94
   virtual State        validate(QString &, int &) const;
 
95
#endif
 
96
   
 
97
   QDZoomValidator(QComboBox * parent, const char * name=0) :
 
98
         QValidator(parent, name) {}
 
99
};
 
100
 
 
101
void
 
102
QDZoomValidator::fixup(QString & str)
 
103
{
 
104
#ifdef QT1
 
105
   str.detach();
 
106
#else
 
107
   str.truncate(0);
 
108
#endif
 
109
 
 
110
   QComboBox * menu=(QComboBox *) parent();
 
111
   menu->setEditText(str=menu->text(menu->currentItem()));
 
112
}
 
113
 
 
114
QValidator::State
 
115
QDZoomValidator::validate(QString & input, int & pos)
 
116
#ifndef QT1
 
117
const
 
118
#endif
 
119
{
 
120
   if (!input.length()) return Valid;
 
121
 
 
122
   char buffer[128];
 
123
   strncpy(buffer, input, 127); buffer[127]=0;
 
124
   char * ptr=buffer+strlen(buffer)-1;
 
125
   while(isspace(*ptr) || *ptr=='%') *ptr--=0;
 
126
   QString str(buffer);
 
127
   
 
128
   bool status;
 
129
   int zoom=str.toInt(&status);
 
130
   if (!status) return Invalid;
 
131
 
 
132
   if (zoom>IDC_ZOOM_MAX-IDC_ZOOM_MIN) return Invalid;
 
133
   if (zoom<=0) return Invalid;
 
134
   if (zoom<5)
 
135
      if (str.length()==1) return Valid;
 
136
      else return Invalid;
 
137
   
 
138
   if (zoom<5 || zoom>IDC_ZOOM_MAX-IDC_ZOOM_MIN) return Invalid;
 
139
   return Acceptable;
 
140
}
 
141
 
 
142
//****************************************************************************
 
143
//***************************** QDTBarModePiece ******************************
 
144
//****************************************************************************
 
145
 
 
146
void
 
147
QDTBarModePiece::setEnabled(bool en)
 
148
{
 
149
   zoom_menu->setEnabled(en);
 
150
   mode_menu->setEnabled(en);
 
151
   zoom_in_butt->setEnabled(en);
 
152
   zoom_out_butt->setEnabled(en);
 
153
 
 
154
   zoom_select_butt->setEnabled(en);
 
155
   text_select_butt->setEnabled(en);
 
156
   pane_butt->setEnabled(en);
 
157
 
 
158
   if ( pin_butt )
 
159
      pin_butt->setEnabled(en);
 
160
}
 
161
 
 
162
static const int menu_items_size=10;
 
163
static const struct MenuItems {
 
164
  const char *str;
 
165
  int zoom;
 
166
} menu_items[menu_items_size] = {
 
167
  {"300 %",IDC_ZOOM_MIN+300},
 
168
  {"150 %",IDC_ZOOM_MIN+150},
 
169
  {"100 %",IDC_ZOOM_MIN+100},
 
170
  {"75 %",IDC_ZOOM_MIN+75},
 
171
  {"50 %",IDC_ZOOM_MIN+50},
 
172
  {"25 %",IDC_ZOOM_MIN+25},
 
173
  {QT_TRANSLATE_NOOP("QDTBarModePiece","Fit Width"),IDC_ZOOM_WIDTH},
 
174
  {QT_TRANSLATE_NOOP("QDTBarModePiece","Fit Page"),IDC_ZOOM_PAGE},
 
175
  {"1 : 1",IDC_ZOOM_ONE2ONE},
 
176
  {QT_TRANSLATE_NOOP("QDTBarModePiece","Stretch"),IDC_ZOOM_STRETCH},
 
177
};
 
178
 
 
179
QDTBarModePiece::QDTBarModePiece(QWidget * toolbar) 
 
180
  : QDTBarPiece(toolbar)
 
181
{
 
182
   if ( dynamic_cast<QDToolBar *>(toolbar) )
 
183
      qdtoolbar_child=TRUE;
 
184
   else
 
185
      qdtoolbar_child=FALSE;
 
186
   
 
187
   mode_menu=new QComboBox(FALSE, toolbar, "mode_menu");
 
188
   mode_menu->insertItem(tr("Color"));
 
189
   mode_menu->insertItem(tr("B&W"));
 
190
   mode_menu->insertItem(tr("Fore"));
 
191
   mode_menu->insertItem(tr("Back"));
 
192
   connect(mode_menu, SIGNAL(activated(int)), this, SLOT(slotMode(int)));
 
193
   QToolTip::add(mode_menu, tr("Display mode"));
 
194
 
 
195
   if ( qdtoolbar_child ) 
 
196
      ((QDToolBar *)toolbar)->addLeftWidget(mode_menu);
 
197
 
 
198
   zoom_menu=new QComboBox(TRUE, toolbar, "zoom_menu");
 
199
   zoom_menu->setInsertionPolicy(QComboBox::NoInsertion);
 
200
   zoom_menu->setValidator(new QDZoomValidator(zoom_menu));
 
201
   int i;
 
202
   for(i=0;i<menu_items_size;i++)
 
203
   {
 
204
     zoom_menu->insertItem(tr(menu_items[i].str));
 
205
   }
 
206
   connect(zoom_menu, SIGNAL(activated(const QString &)),
 
207
           this, SLOT(slotZoom(const QString &)));
 
208
   QToolTip::add(zoom_menu, tr("Zoom"));
 
209
   if ( qdtoolbar_child )
 
210
      ((QDToolBar *)toolbar)->addLeftWidget(zoom_menu);
 
211
 
 
212
   zoom_in_butt=new QDToolButton(*CINData::get("ppm_vzoom_in"), true,
 
213
                                 IDC_ZOOM_ZOOMIN, toolbar, tr("Zoom In"));
 
214
   connect(zoom_in_butt, SIGNAL(clicked(void)), this, SLOT(slotZoom(void)));
 
215
 
 
216
   zoom_out_butt=new QDToolButton(*CINData::get("ppm_vzoom_out"), true,
 
217
                                  IDC_ZOOM_ZOOMOUT, toolbar, tr("Zoom Out"));
 
218
   connect(zoom_out_butt, SIGNAL(clicked(void)), this, SLOT(slotZoom(void)));
 
219
 
 
220
   if ( qdtoolbar_child ) 
 
221
      ((QDToolBar *)toolbar)->addLeftWidgets(zoom_in_butt, zoom_out_butt);
 
222
 
 
223
   pane_butt=new QDToolButton(*CINData::get("ppm_hand1"), true,
 
224
                                  IDC_PANE, toolbar, tr("Pane Mode"));
 
225
   pane_butt->setToggleButton(TRUE);
 
226
   connect(pane_butt, SIGNAL(clicked(void)), 
 
227
           this, SLOT(slotPaneMode(void)));
 
228
 
 
229
   zoom_select_butt=new QDToolButton(*CINData::get("ppm_zoomselect"), true,
 
230
                                     IDC_ZOOM_SELECT, toolbar,
 
231
                                     tr("Zoom Selected Area"));
 
232
   zoom_select_butt->setToggleButton(TRUE);
 
233
   connect(zoom_select_butt, SIGNAL(clicked(void)), 
 
234
           this, SLOT(slotPaneMode(void)));
 
235
   
 
236
   text_select_butt=new QDToolButton(*CINData::get("ppm_textselect"), true,
 
237
                                     IDC_TEXT_SELECT, toolbar, 
 
238
                                     tr("Select Text in Selected Area"));
 
239
   text_select_butt->setToggleButton(TRUE);
 
240
   connect(text_select_butt, SIGNAL(clicked(void)), 
 
241
           this, SLOT(slotPaneMode(void)));
 
242
   
 
243
   if ( qdtoolbar_child ) 
 
244
     ((QDToolBar *)toolbar)->addLeftWidgets(pane_butt, 
 
245
                                            zoom_select_butt, 
 
246
                                            text_select_butt);
 
247
   
 
248
   if ( qdtoolbar_child )
 
249
     {
 
250
       pin_butt=new QDToolButton(*CINData::get("ppm_vpin_out"), false,
 
251
                                 -1, toolbar, tr("Stick"));
 
252
       pin_butt->setToggleButton(TRUE);
 
253
       pin_butt->setOnPixmap(*CINData::get("ppm_vpin_in"));
 
254
       connect(pin_butt, SIGNAL(toggled(bool)), this, SIGNAL(sigStick(bool)));
 
255
       ((QDToolBar *)toolbar)->addRightWidget(pin_butt);
 
256
       
 
257
       ((QDToolBar *)toolbar)->addPiece(this);
 
258
     }
 
259
   else
 
260
     {
 
261
       pin_butt=NULL;
 
262
     }
 
263
}
 
264
 
 
265
 
 
266
void
 
267
QDTBarModePiece::setOptions(int opts)
 
268
{
 
269
  bool b;
 
270
  b = !(opts & QDBase::OverrideFlags::TOOLBAR_NO_RESCOMBO);
 
271
  showOrHide(zoom_menu, b);
 
272
  b = !(opts & QDBase::OverrideFlags::TOOLBAR_NO_DISPCOMBO);
 
273
  showOrHide(mode_menu, b);
 
274
  b = !(opts & QDBase::OverrideFlags::TOOLBAR_NO_ZOOM);
 
275
  showOrHide(zoom_in_butt, b);
 
276
  showOrHide(zoom_out_butt, b);
 
277
  b = !(opts & QDBase::OverrideFlags::TOOLBAR_NO_PAN);
 
278
  showOrHide(pane_butt, b);
 
279
  b = !(opts & QDBase::OverrideFlags::TOOLBAR_NO_ZOOMSEL);
 
280
  showOrHide(zoom_select_butt, b);
 
281
  b = !(opts & QDBase::OverrideFlags::TOOLBAR_NO_TEXTSEL);
 
282
  showOrHide(text_select_butt, b);
 
283
}
 
284
 
 
285
 
 
286
void
 
287
QDTBarModePiece::update(int cmd_mode, bool mode_enabled, int cmd_zoom, int zoom,
 
288
                        int pane_mode, int has_text)
 
289
{
 
290
   int i;
 
291
   for(i=0;i<menu_items_size;i++)
 
292
   {
 
293
     if(cmd_zoom == menu_items[i].zoom)
 
294
     {
 
295
       zoom_menu->setCurrentItem(i);
 
296
       break;
 
297
     }
 
298
   }
 
299
   if(i==menu_items_size)
 
300
   {
 
301
      char buffer[64];
 
302
      sprintf(buffer, "%d %%", zoom);
 
303
      zoom_menu->setEditText(buffer);
 
304
   }
 
305
 
 
306
   zoom_in_butt->setEnabled(zoom<IDC_ZOOM_MAX-IDC_ZOOM_MIN);
 
307
   zoom_out_butt->setEnabled(zoom>5);
 
308
 
 
309
   pane_butt->setOn(pane_mode==IDC_PANE);
 
310
   zoom_select_butt->setOn(pane_mode==IDC_ZOOM_SELECT);
 
311
   if (has_text)
 
312
   {
 
313
      if (!text_select_butt->isEnabled())
 
314
         text_select_butt->setEnabled(true);
 
315
      text_select_butt->setOn(pane_mode==IDC_TEXT_SELECT);
 
316
   }
 
317
   else
 
318
   {
 
319
      if (text_select_butt->isEnabled())
 
320
         text_select_butt->setEnabled(false);
 
321
   }
 
322
   
 
323
   
 
324
   switch(cmd_mode)
 
325
   {
 
326
      case IDC_DISPLAY_COLOR:
 
327
        mode_menu->setCurrentItem(0);
 
328
        break;
 
329
      case IDC_DISPLAY_BLACKWHITE:
 
330
        mode_menu->setCurrentItem(1);
 
331
        break;
 
332
      case IDC_DISPLAY_FOREGROUND:
 
333
        mode_menu->setCurrentItem(2);
 
334
        break;
 
335
      case IDC_DISPLAY_BACKGROUND:
 
336
        mode_menu->setCurrentItem(3);
 
337
        break;
 
338
      default:
 
339
        break;
 
340
   }
 
341
 
 
342
   if ( !qdtoolbar_child )
 
343
      zoom_menu->setEnabled(true);
 
344
   
 
345
   mode_menu->setEnabled(mode_enabled);
 
346
 
 
347
   zoom_menu->setFixedSize(zoom_menu->sizeHint());
 
348
   mode_menu->setFixedSize(mode_menu->sizeHint());
 
349
 
 
350
      // Keep everything disabled if the toolbar is disabled.
 
351
   if (!toolbar->isEnabled()) setEnabled(false);
 
352
}
 
353
 
 
354
bool
 
355
QDTBarModePiece::isStuck(void) const
 
356
{
 
357
   if ( !pin_butt ) return FALSE;
 
358
   return pin_butt->isOn();
 
359
}
 
360
 
 
361
void
 
362
QDTBarModePiece::stick(bool en)
 
363
{
 
364
   if ( pin_butt )
 
365
      pin_butt->setOn(en);
 
366
}
 
367
 
 
368
void
 
369
QDTBarModePiece::slotZoom(const QString & qstr)
 
370
{
 
371
  int i;
 
372
  for(i=0;i<menu_items_size;i++)
 
373
  {
 
374
    if(qstr==tr(menu_items[i].str))
 
375
    {
 
376
      emit sigSetZoom(menu_items[i].zoom);
 
377
      break;
 
378
    }
 
379
  }
 
380
  if(i == menu_items_size)
 
381
  {
 
382
     const char * const str=qstr;
 
383
    i=atoi(str);
 
384
    if(i>0)
 
385
    {
 
386
      emit sigSetZoom(i+IDC_ZOOM_MIN);
 
387
    }
 
388
  }
 
389
}
 
390
 
 
391
void
 
392
QDTBarModePiece::slotZoom(void)
 
393
{
 
394
   const QObject * obj=sender();
 
395
   if (obj && obj->isWidgetType() && obj->inherits("QDToolButton"))
 
396
   {
 
397
      const QDToolButton * butt=(QDToolButton *) obj;
 
398
      emit sigSetZoom(butt->cmd);
 
399
   }
 
400
}
 
401
 
 
402
void
 
403
QDTBarModePiece::slotPaneMode(void)
 
404
{
 
405
   const QObject * obj=sender();
 
406
   if (obj && obj->inherits("QDToolButton") && ((QDToolButton *)obj)->isToggleButton())
 
407
   {
 
408
      QDToolButton * butt=(QDToolButton *) obj;
 
409
      
 
410
      if (butt->isOn())
 
411
      {
 
412
         // a bit inefficient, but I don't want to use button group either
 
413
         zoom_select_butt->setOn(FALSE);
 
414
         text_select_butt->setOn(FALSE);
 
415
         pane_butt->setOn(FALSE);
 
416
      }
 
417
      butt->setOn(TRUE);
 
418
      emit sigSetPaneMode(butt->cmd);
 
419
   }
 
420
}
 
421
 
 
422
void
 
423
QDTBarModePiece::slotMode(int index)
 
424
{
 
425
   switch(index)
 
426
   {
 
427
      case 0:
 
428
        emit sigSetMode(IDC_DISPLAY_COLOR);
 
429
        break;
 
430
      case 1:
 
431
        emit sigSetMode(IDC_DISPLAY_BLACKWHITE);
 
432
        break;
 
433
      case 2:
 
434
        emit sigSetMode(IDC_DISPLAY_FOREGROUND);
 
435
        break;
 
436
      case 3:
 
437
        emit sigSetMode(IDC_DISPLAY_BACKGROUND);
 
438
        break;
 
439
      default:
 
440
        break;
 
441
   }
 
442
}
 
443
 
 
444
// END OF FILE