summaryrefslogtreecommitdiffstats
path: root/superkaramba/src/bar.cpp
blob: 354433dc5ebef573f65b6c48d0989b010818edcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/***************************************************************************
 *   Copyright (C) 2003 by Hans Karlsson                                   *
 *   karlsson.h@home.se                                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "bar.h"
#include "karamba.h"

Bar::Bar(karamba* k, int x, int y, int w, int h) : Meter(k, x, y, w, h)
{
  value = 0;
  minValue = 0;
  maxValue = 100;
  barValue = 0;
  vertical = false;
}

Bar::~Bar()
{
}

bool Bar::setImage(QString fileName)
{
  QFileInfo fileInfo(fileName);
  bool res = false;

  if(m_karamba->theme().isThemeFile(fileName))
  {
    QByteArray ba = m_karamba->theme().readThemeFile(fileName);
    res = pixmap.loadFromData(ba);
  }
  else
  {
    res = pixmap.load(fileName);
  }
  pixmapWidth = pixmap.width();
  pixmapHeight = pixmap.height();

  if(getWidth()==0 || getHeight()==0)
  {
    setWidth(pixmapWidth);
    setHeight(pixmapHeight);
  }
  if(res)
    imagePath = fileName;
  return res;
}

void Bar::setValue( long v )
{
  if(v > maxValue)
  {
    // maxValue = v;
    v = maxValue;
  }

  if(v < minValue)
  {
    //minValue = v;
    v = minValue;
  }

  barValue = v;

  long diff = maxValue - minValue;
  if(diff != 0)
  {
    if(vertical)
    {
      value = long((v-minValue)*getHeight() / diff + 0.5);
    }
    else // horizontal
    {
      value = long((v-minValue)*getWidth() / diff + 0.5);
    }
  }
  else
  {
    value = 0;
  }
}

void Bar::setValue(QString v)
{
  setValue((long)(v.toDouble() + 0.5));
}

void Bar::setMax(long m)
{
  Meter::setMax(m);
  recalculateValue();
}

void Bar::setMin(long m)
{
  Meter::setMin(m);
  recalculateValue();
}

void Bar::setVertical(bool b)
{
  vertical = b;
}

void Bar::mUpdate(QPainter *p)
{
  int x, y, width, height;
  x = getX();
  y = getY();
  width = getWidth();
  height = getHeight();
  //only draw image if not hidden
  if(hidden == 0)
  {
    if(vertical)
    {
      //  int v = int( (value-minValue)*height / (maxValue-minValue) + 0.5 );
      p->drawTiledPixmap(x, y+height-value, width, value, pixmap, 0,
                         pixmapHeight-value);
    }
    else // horizontal
    {
      //int v = int( (value-minValue)*width / (maxValue-minValue) + 0.5 );
      p->drawTiledPixmap(x, y, value, height, pixmap);
    }
  }
}

#include "bar.moc"