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

« back to all changes in this revision

Viewing changes to muse/widgets/utils.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Eric Hedekar, Eric Hedekar, Fabrice Coutadeur
  • Date: 2010-01-26 02:32:14 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20100126023214-8ez2g5d26d9p584j
Tags: 1.0.1-0ubuntu1
[ Eric Hedekar ]
* New upstream version (LP: #479688)
* Removed patches that were fixed in upstream source
  -[10_64bit_memcorruption_fix]
  -[10_gcc43_build_fixes]
  -[10_lash_private_api_fix]
  -[10_log2f_aliasing_fix]
  -[10_vamgui_init_fix]
  -[20_fix_const]

[ Fabrice Coutadeur ]
* debian/watch: added watch file
* debian/muse.desktop: deleted deprecated Encoding key, Application category
  and icon extension. This fix several warning with lintian and
  desktop-file-validate

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
//=========================================================
2
2
//  MusE
3
3
//  Linux Music Editor
4
 
//    $Id: utils.cpp,v 1.1.1.1 2003/10/27 18:55:11 wschweer Exp $
 
4
//    $Id: utils.cpp,v 1.1.1.1.2.3 2009/11/14 03:37:48 terminator356 Exp $
5
5
//  (C) Copyright 1999 Werner Schweer (ws@seh.de)
6
6
//=========================================================
7
7
 
11
11
#include <qframe.h>
12
12
#include <sys/time.h>
13
13
 
 
14
// Quick bit-shift lookup table
 
15
const unsigned int bitShiftLU[32] = { 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, 
 
16
                                      0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000, 0x8000,
 
17
                                      0x10000, 0x20000, 0x40000, 0x80000, 0x100000, 0x200000, 0x400000, 0x800000, 
 
18
                                      0x1000000, 0x2000000, 0x4000000, 0x8000000, 0x10000000, 0x20000000, 0x40000000, 0x80000000 };  
 
19
 
14
20
//---------------------------------------------------------
15
21
//   curTime
16
22
//---------------------------------------------------------
93
99
            int first = 0;
94
100
            bool needSpace = false;
95
101
            bm &= 0xffff;
96
 
            for (int i = 0; i < 17; ++i) {
 
102
            //for (int i = 0; i < 17; ++i) {
 
103
            for (int i = 0; i < 16; ++i) {
97
104
                  if ((1 << i) & bm) {
98
105
                        if (!range) {
99
106
                              range = true;
121
128
      }
122
129
 
123
130
//---------------------------------------------------------
 
131
//   u32bitmap2String
 
132
//---------------------------------------------------------
 
133
// Added by Tim. p3.3.8
 
134
 
 
135
QString u32bitmap2String(unsigned int bm)
 
136
      {
 
137
      QString s;
 
138
//printf("bitmap2string: bm %04x", bm);
 
139
      //if (bm == 0xffff)
 
140
      if (bm == 0xffffffff)
 
141
            s = "all";
 
142
      else if (bm == 0)
 
143
            s = "none";
 
144
      else {
 
145
            bool range = false;
 
146
            int first = 0;
 
147
            //unsigned int first = 0;
 
148
            bool needSpace = false;
 
149
            //bm &= 0xffff;
 
150
            //for (int i = 0; i < 17; ++i) {
 
151
            for (int i = 0; i < 33; ++i) {
 
152
                  if ((i < 32) && ((1U << i) & bm)) {
 
153
                        if (!range) {
 
154
                              range = true;
 
155
                              first = i;
 
156
                              }
 
157
                        }
 
158
                  else {
 
159
                        if (range) {
 
160
                              if (needSpace)
 
161
                                    s += " ";
 
162
                              QString ns;
 
163
                              if (first == i-1)
 
164
                                    ns.sprintf("%d", first+1);
 
165
                                    //ns.sprintf("%u", first+1);
 
166
                              else
 
167
                                    ns.sprintf("%d-%d", first+1, i);
 
168
                                    //ns.sprintf("%u-%u", first+1, i);
 
169
                              s += ns;
 
170
                              needSpace = true;
 
171
                              }
 
172
                        range = false;
 
173
                        }
 
174
                  }
 
175
            }
 
176
//printf(" -> <%s>\n", s.latin1());
 
177
      return s;
 
178
      }
 
179
 
 
180
//---------------------------------------------------------
124
181
//   string2bitmap
125
182
//---------------------------------------------------------
126
183
 
176
233
      return val & 0xffff;
177
234
      }
178
235
 
179
 
 
180
 
 
 
236
//---------------------------------------------------------
 
237
//   string2u32bitmap
 
238
//---------------------------------------------------------
 
239
// Added by Tim. p3.3.8
 
240
 
 
241
unsigned int string2u32bitmap(const QString& str)
 
242
      {
 
243
      //int val = 0;
 
244
      unsigned int val = 0;
 
245
      QString ss = str.simplifyWhiteSpace();
 
246
      const char* s = ss.latin1();
 
247
//printf("string2bitmap <%s>\n", s);
 
248
 
 
249
      if (s == 0)
 
250
            return 0;
 
251
      if (strcmp(s, "all") == 0)
 
252
            //return 0xffff;
 
253
            return 0xffffffff;
 
254
      if (strcmp(s, "none") == 0)
 
255
            return 0;
 
256
// printf("str2bitmap: <%s> ", str.latin1);
 
257
      int tval   = 0;
 
258
      //unsigned int tval   = 0;
 
259
      bool range = false;
 
260
      int sval   = 0;
 
261
      //unsigned int sval   = 0;
 
262
      while (*s == ' ')
 
263
            ++s;
 
264
      while (*s) {
 
265
            if (*s >= '0'  && *s <= '9') {
 
266
                  tval *= 10;
 
267
                  tval += *s - '0';
 
268
                  }
 
269
            else if (*s == ' ' || *s == ',') {
 
270
                  if (range) {
 
271
                        for (int i = sval-1; i < tval; ++i)
 
272
                        //for (unsigned int i = sval-1; i < tval; ++i)
 
273
                              val |= (1U << i);
 
274
                        range = false;
 
275
                        }
 
276
                  else {
 
277
                        val |= (1U << (tval-1));
 
278
                        }
 
279
                  tval = 0;
 
280
                  }
 
281
            else if (*s == '-') {
 
282
                  range = true;
 
283
                  sval  = tval;
 
284
                  tval  = 0;
 
285
                  }
 
286
            ++s;
 
287
            }
 
288
      if (range && tval) {
 
289
            for (int i = sval-1; i < tval; ++i)
 
290
            //for (unsigned int i = sval-1; i < tval; ++i)
 
291
                  val |= (1U << i);
 
292
            }
 
293
      else if (tval) {
 
294
            val |= (1U << (tval-1));
 
295
            }
 
296
      //return val & 0xffff;
 
297
      return val;
 
298
      }
 
299
 
 
300
//---------------------------------------------------------
 
301
//   autoAdjustFontSize
 
302
//   w: Widget to auto adjust font size
 
303
//   s: String to fit
 
304
//   ignoreWidth: Set if dealing with a vertically constrained widget - one which is free to resize horizontally.
 
305
//   ignoreHeight: Set if dealing with a horizontally constrained widget - one which is free to resize vertically. 
 
306
//---------------------------------------------------------
 
307
// Added by Tim. p3.3.8
 
308
 
 
309
bool autoAdjustFontSize(QFrame* w, const QString& s, bool ignoreWidth, bool ignoreHeight, int max, int min)
 
310
{
 
311
  // In case the max or min was obtained from QFont::pointSize() which returns -1 
 
312
  //  if the font is a pixel font, or if min is greater than max...
 
313
  if(!w || (min < 0) || (max < 0) || (min > max))
 
314
    return false;
 
315
    
 
316
  // Limit the minimum and maximum sizes to something at least readable.
 
317
  if(max < 4)
 
318
    max = 4;
 
319
  if(min < 4)
 
320
    min = 4;
 
321
    
 
322
  QRect cr = w->contentsRect();
 
323
  QRect r;
 
324
  QFont fnt = w->font();
 
325
  // An extra amount just to be sure - I found it was still breaking up two words which would fit on one line.
 
326
  int extra = 4;
 
327
  // Allow at least one loop. min can be equal to max.
 
328
  for(int i = max; i >= min; --i)
 
329
  {
 
330
    fnt.setPointSize(i);
 
331
    QFontMetrics fm(fnt);
 
332
    r = fm.boundingRect(s);
 
333
    // Would the text fit within the widget?
 
334
    if((ignoreWidth || (r.width() <= (cr.width() - extra))) && (ignoreHeight || (r.height() <= cr.height())))
 
335
      break;
 
336
  }
 
337
  // Added by Tim. p3.3.9
 
338
  //printf("autoAdjustFontSize: ptsz:%d widget:%s before setFont x:%d y:%d w:%d h:%d\n", fnt.pointSize(), w->name(), w->x(), w->y(), w->width(), w->height());
 
339
  
 
340
  // Here we will always have a font ranging from min to max point size.
 
341
  w->setFont(fnt);
 
342
  // Added by Tim. p3.3.9
 
343
  //printf("autoAdjustFontSize: ptsz:%d widget:%s x:%d y:%d w:%d h:%d frame w:%d rw:%d rh:%d\n", fnt.pointSize(), w->name(), w->x(), w->y(), w->width(), w->height(), w->frameWidth(), cr.width(), cr.height());
 
344
  
 
345
  // Force minimum height. Use the expected height for the highest given point size.
 
346
  // This way the mixer strips aren't all different label heights, but can be larger if necessary.
 
347
  // Only if ignoreHeight is set (therefore the height is adjustable).
 
348
  if(ignoreHeight)
 
349
  {
 
350
    fnt.setPointSize(max);
 
351
    QFontMetrics fm(fnt);
 
352
    // Set the label's minimum height equal to the height of the font.
 
353
    w->setMinimumHeight(fm.height() + 2 * w->frameWidth());
 
354
  }
 
355
  
 
356
  return true;  
 
357
}