~ubuntu-branches/ubuntu/quantal/muse/quantal

« back to all changes in this revision

Viewing changes to muse/widgets/sigedit.cpp

  • Committer: Package Import Robot
  • Author(s): Alessio Treglia
  • Date: 2011-08-12 11:16:41 UTC
  • mfrom: (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 26.
  • Revision ID: package-import@ubuntu.com-20110812111641-sg7bj019yhh91mpl
Tags: upstream-2.0~beta2
ImportĀ upstreamĀ versionĀ 2.0~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
//  (C) Copyright 2001 Werner Schweer (ws@seh.de)
6
6
//=========================================================
7
7
 
 
8
#include <stdio.h>
 
9
#include <values.h>
 
10
 
 
11
#include <QEvent>
 
12
#include <QKeyEvent>
 
13
#include <QList>
 
14
#include <QMouseEvent>
 
15
#include <QPainter>
 
16
#include <QPaintEvent>
 
17
#include <QPixmap>
 
18
#include <QResizeEvent>
 
19
#include <QString>
 
20
#include <QStyle>
 
21
#include <QTimerEvent>
 
22
 
 
23
///#include "sig.h"
 
24
#include "al/sig.h"
8
25
#include "sigedit.h"
9
 
 
10
 
#include <qrangecontrol.h>
11
 
#include <qapplication.h>
12
 
#include <qpixmap.h>
13
 
#include <qapplication.h>
14
 
#include <qvaluelist.h>
15
 
#include <qstring.h>
16
 
#include <qstyle.h>
17
 
#include <values.h>
18
 
#include <qpainter.h>
19
 
#include <qtimer.h>
20
 
#include "sig.h"
 
26
#include "spinbox.h"
21
27
 
22
28
extern int mtcType;
23
29
 
67
73
//   SigEditor
68
74
//---------------------------------------------------------
69
75
 
70
 
class SigEditor : public QWidget
 
76
class SigEditor : public QLineEdit
71
77
      {
72
78
      SigEdit* cw;
73
79
      bool frm;
74
80
      QPixmap *pm;
75
81
      int focusSec;
76
 
      QValueList<NumberSection> sections;
 
82
      QList<NumberSection> sections;
77
83
      int offset;
78
84
 
79
85
      int section(const QPoint&);
84
90
      void resizeEvent(QResizeEvent*);
85
91
      void paintEvent(QPaintEvent*);
86
92
      void mousePressEvent(QMouseEvent *e);
 
93
      void keyPressEvent(QKeyEvent * event );
87
94
      void applyFocusSelection() {}
88
95
 
89
96
   public:
99
106
      void appendSection(const NumberSection& sec);
100
107
      void clearSections();
101
108
      void setSectionSelection(int sec, int selstart, int selend);
102
 
      bool eventFilter(QObject *o, QEvent *e);
103
109
      };
104
110
 
105
111
//---------------------------------------------------------
111
117
      if (pm->isNull())
112
118
            return -1;
113
119
      QPainter p(pm);
114
 
      int fw = frm ? style().pixelMetric(QStyle::PM_DefaultFrameWidth) : 0;
 
120
      int fw = frm ? style()->pixelMetric(QStyle::PM_DefaultFrameWidth) : 0;
115
121
      int x = 2 + fw;
116
122
      int y = 0;
117
123
      int w = width();
118
124
      int h = height();
119
 
      for (unsigned int i = 0; i < sections.count(); ++i) {
 
125
      for (int i = 0; i < sections.count(); ++i) {
120
126
            QString s = cw->sectionFormattedText(i);
121
 
            QRect bb = p.boundingRect(x, y, w, h, AlignVCenter|AlignLeft, s);
 
127
            QRect bb = p.boundingRect(x, y, w, h, Qt::AlignVCenter|Qt::AlignLeft, s);
122
128
            int nx = bb.x() + bb.width();
123
129
            if (pt.x() >= x && pt.x() < nx)
124
130
                  return i;
125
131
            x = nx;
126
132
            if (i < sections.count()-1) {
127
133
                  QString s("/");
128
 
                  p.drawText(x, y, w, h, AlignVCenter|AlignLeft, s, -1, &bb);
 
134
                  p.drawText(x, y, w, h, Qt::AlignVCenter|Qt::AlignLeft, s, -1, &bb);
129
135
                  x = bb.x() + bb.width();
130
136
                  }
131
137
            }
137
143
//---------------------------------------------------------
138
144
 
139
145
SigEditor::SigEditor(SigEdit* parent, const char* name)
140
 
   : QWidget(parent, name)
 
146
   : QLineEdit(parent)
141
147
      {
 
148
      setObjectName(name);
142
149
      cw       = parent;
143
150
      frm      = true;
144
151
      focusSec = 0;
162
169
 
163
170
void SigEditor::init()
164
171
      {
165
 
      setBackgroundMode(PaletteBase);
 
172
      setBackgroundMode(Qt::PaletteBase);
166
173
      setFocusSection(-1);
167
174
      setKeyCompression(true);
168
 
      installEventFilter(this);
169
 
      setFocusPolicy(WheelFocus);
 
175
      setFocusPolicy(Qt::WheelFocus);
170
176
      }
171
177
 
172
178
//---------------------------------------------------------
178
184
      if (e->type() == QEvent::FocusIn || e->type() == QEvent::FocusOut) {
179
185
            repaint( rect(), false);
180
186
            }
181
 
      else if (e->type() == QEvent::AccelOverride) {
 
187
      else if (e->type() == QEvent::ShortcutOverride) {
182
188
            QKeyEvent* ke = (QKeyEvent*) e;
183
189
            switch (ke->key()) {
184
 
                  case Key_Delete:
185
 
                  case Key_Backspace:
186
 
                  case Key_Up:
187
 
                  case Key_Down:
188
 
                  case Key_Left:
189
 
                  case Key_Right:
 
190
                  case Qt::Key_Delete:
 
191
                  case Qt::Key_Backspace:
 
192
                  case Qt::Key_Up:
 
193
                  case Qt::Key_Down:
 
194
                  case Qt::Key_Left:
 
195
                  case Qt::Key_Right:
190
196
                        ke->accept();
191
197
                  default:
192
198
                        break;
193
199
                  }
194
200
            }
195
 
      return QWidget::event(e);
 
201
      return QLineEdit::event(e);
196
202
      }
197
203
 
198
204
void SigEditor::resizeEvent(QResizeEvent *e)
199
205
      {
200
206
      pm->resize(e->size());
201
 
      QWidget::resizeEvent(e);
 
207
      QLineEdit::resizeEvent(e);
202
208
      }
203
209
 
204
210
//---------------------------------------------------------
215
221
      p.setPen(colorGroup().text());
216
222
      QBrush bg = cg.brush(QColorGroup::Base);
217
223
 
218
 
      int fw = frm ? style().pixelMetric(QStyle::PM_DefaultFrameWidth) : 0;
 
224
      int fw = frm ? style()->pixelMetric(QStyle::PM_DefaultFrameWidth) : 0;
219
225
      int x = 2 + fw;
220
226
      int y = 0;
221
227
      int w = width();
222
228
      int h = height();
223
229
      p.fillRect(0, 0, w, h, bg);
224
230
 
225
 
      for (unsigned int i = 0; i < sections.count(); ++i) {
 
231
      for (int i = 0; i < sections.count(); ++i) {
226
232
            QRect bb;
227
233
            QString s = cw->sectionFormattedText(i);
228
234
 
229
235
            if (hasFocus() && (int(i) == focusSec)) {
230
236
                  QBrush bg = cg.brush(QColorGroup::Highlight);
231
 
                  QRect r = p.boundingRect(x, y, w, h, AlignVCenter|AlignLeft, s, -1);
 
237
                  QRect r = p.boundingRect(x, y, w, h, Qt::AlignVCenter|Qt::AlignLeft, s, -1);
232
238
                  p.setPen(colorGroup().highlightedText());
233
239
                  p.fillRect(r, bg);
234
240
                  }
235
241
            else
236
242
                  p.setPen(colorGroup().text());
237
 
            p.drawText(x, y, w, h, AlignVCenter|AlignLeft, s, -1, &bb);
 
243
            p.drawText(x, y, w, h, Qt::AlignVCenter|Qt::AlignLeft, s, -1, &bb);
238
244
            x = bb.x() + bb.width();
239
245
            if (i < sections.count()-1) {
240
246
                  QString s("/");
241
 
                  p.drawText(x, y, w, h, AlignVCenter|AlignLeft, s, -1, &bb);
 
247
                  p.drawText(x, y, w, h, Qt::AlignVCenter|Qt::AlignLeft, s, -1, &bb);
242
248
                  x = bb.x() + bb.width();
243
249
                  }
244
250
            }
261
267
      }
262
268
 
263
269
//---------------------------------------------------------
264
 
//   eventFilter
 
270
//   keyPressEvent
265
271
//---------------------------------------------------------
266
272
 
267
 
bool SigEditor::eventFilter(QObject *o, QEvent *e)
 
273
void SigEditor::keyPressEvent(QKeyEvent * e )
268
274
      {
269
 
      if (o != this)
270
 
            return false;
271
 
      if (e->type() != QEvent::KeyPress )
272
 
            return false;
273
 
 
274
 
      QKeyEvent *ke = (QKeyEvent*)e;
275
 
      switch (ke->key()) {
276
 
            case Key_Right:
 
275
      switch (e->key()) {
 
276
            case Qt::Key_Right:
277
277
                  if (unsigned(focusSec) <= sections.count()) {
278
278
                        if (cw->setFocusSection(focusSec+1))
279
279
                              repaint(rect(), false);
280
280
                        }
281
 
                  return true;
282
 
            case Key_Left:
 
281
            case Qt::Key_Left:
283
282
                  if (focusSec > 0 ) {
284
283
                        if (cw->setFocusSection(focusSec-1))
285
284
                              repaint(rect(), false);
286
285
                        }
287
 
                  return true;
288
 
            case Key_Up:
 
286
            case Qt::Key_Up:
289
287
                  cw->stepUp();
290
 
                  return true;
291
 
            case Key_Down:
 
288
            case Qt::Key_Down:
292
289
                  cw->stepDown();
293
 
                  return true;
294
 
            case Key_Backspace:
295
 
            case Key_Delete:
 
290
            case Qt::Key_Backspace:
 
291
            case Qt::Key_Delete:
296
292
                  cw->removeLastNumber(focusSec);
297
 
                  return true;
298
 
            case Key_Enter:
299
 
            case Key_Return:
 
293
            case Qt::Key_Enter:
 
294
            case Qt::Key_Return:
300
295
                  cw->enterPressed();
301
 
                  return true;
302
296
            default:
303
 
                  QString txt = ke->text();
 
297
                  QString txt = e->text();
304
298
                  if (!txt.isEmpty() && txt[0] == '/') {
305
299
                        // do the same thing as KEY_RIGHT when the user presses the separator key
306
300
                        if (focusSec < (signed)(sections.count())) {
307
301
                              if (cw->setFocusSection(focusSec+1))
308
302
                                    repaint(rect(), false);
309
303
                              }
310
 
                        return true;
311
304
                        }
312
305
                  int num = txt[0].digitValue();
313
306
                  
314
 
                  //printf("SigEditor::eventFilter num:%d\n", num);
 
307
                  //printf("SigEditor::keyPressEvent num:%d\n", num);
315
308
                  
316
309
                  if (num != -1) {
317
310
                        cw->addNumber(focusSec, num);
318
 
                        return true;
319
311
                        }
320
312
            }
321
 
      return false;
322
313
      }
323
314
 
324
315
void SigEditor::appendSection(const NumberSection& sec)
363
354
//---------------------------------------------------------
364
355
 
365
356
SigEdit::SigEdit(QWidget* parent, const char* name)
366
 
   : QWidget(parent, name)
 
357
   : QWidget(parent)
367
358
      {
 
359
      setObjectName(name);
368
360
      init();
369
361
      updateButtons();
370
362
      }
380
372
void SigEdit::init()
381
373
      {
382
374
      ed       = new SigEditor(this, "pos editor");
383
 
      controls = new QSpinWidget(this, "pos edit controls");
384
 
      controls->setEditWidget(ed);
 
375
      controls = new SpinBox(this);
 
376
      controls->setEditor(ed);
385
377
      setFocusProxy(ed);
386
378
      connect(controls, SIGNAL(stepUpPressed()), SLOT(stepUp()));
387
379
      connect(controls, SIGNAL(stepDownPressed()), SLOT(stepDown()));
422
414
void SigEdit::setValue(const QString& s)
423
415
      {
424
416
      int z, n;
425
 
      sscanf(s.latin1(), "%d/%d", &z, &n);
 
417
      sscanf(s.toLatin1(), "%d/%d", &z, &n);
426
418
      Sig sig(z, n);
427
419
      setValue(sig);
428
420
      }
632
624
 
633
625
      QString txt = sectionText(secNo);
634
626
 
635
 
      //printf("SigEdit::addNumber secNo:%d num:%d voff:%d txt:%s\n", secNo, num, voff, txt.latin1());
 
627
      //printf("SigEdit::addNumber secNo:%d num:%d voff:%d txt:%s\n", secNo, num, voff, txt.toLatin1());
636
628
      
637
 
      if (txt.length() == sec[secNo].len) {
 
629
      if ((unsigned) txt.length() == sec[secNo].len) {
638
630
            //printf("SigEdit::addNumber txt.length() == sec[secNo].len (%d)\n", sec[secNo].len);
639
631
      
640
632
            if (!outOfRange(secNo, num - voff)) {
651
643
            int temp = txt.toInt() - voff;
652
644
            if (outOfRange(secNo, temp))
653
645
            {
654
 
                  //printf("SigEdit::addNumber not accepted secNo:%d txt:%s temp:%d\n", secNo, txt.latin1(), temp);
 
646
                  //printf("SigEdit::addNumber not accepted secNo:%d txt:%s temp:%d\n", secNo, txt.toLatin1(), temp);
655
647
                  
656
648
                  txt = sectionText(secNo);
657
649
            }
661
653
                  accepted = true;
662
654
                  sec[secNo].val = temp;
663
655
                  }
664
 
            if (adv && (txt.length() == sec[secNo].len)) {
 
656
            if (adv && ((unsigned) txt.length() == sec[secNo].len)) {
665
657
                  setFocusSection(ed->focusSection() + 1);
666
658
                  }
667
659
            }
702
694
QSize SigEdit::sizeHint() const
703
695
      {
704
696
      QFontMetrics fm(font());
705
 
      int fw = style().pixelMetric(QStyle::PM_DefaultFrameWidth, this);
 
697
      int fw = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, 0, this); // ddskrjo
706
698
      int h  = fm.height() + fw * 2;
707
 
      int w  = 2 + controls->upRect().width() + fw * 4;
 
699
 
 
700
      int w  = 2 + controls->arrowWidth() + fw * 4;
708
701
      w     += fm.width('9') * 5 + fm.width('/');
709
702
      return QSize(w, h).expandedTo(QApplication::globalStrut());
710
703
      }
733
726
                  case 128: upEnabled = false; break;
734
727
                  }
735
728
            }
736
 
      controls->setUpEnabled(isEnabled() && upEnabled);
737
 
      controls->setDownEnabled(isEnabled() && downEnabled);
 
729
      controls->setStepEnabled(isEnabled() & upEnabled, isEnabled() & downEnabled);
738
730
      }
739
731
 
740
732
//---------------------------------------------------------