summaryrefslogtreecommitdiffstats
path: root/kvoctrain/kvoctrain/statistik-dialogs/StatistikPage.cpp
blob: ae34d1918362804d3fcdedb2f1e05893685f7f8f (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/***************************************************************************

                       statistics dialog page

    -----------------------------------------------------------------------

    begin          : Thu Sep 21 20:50:53 MET 1999

    copyright      : (C) 1999-2001 Ewald Arnold <kvoctrain@ewald-arnold.de>
                     (C) 2001 The KDE-EDU team
                     (C) 2005 Peter Hedlund <peter.hedlund@kdemail.net>

    -----------------------------------------------------------------------

 ***************************************************************************

 ***************************************************************************
 *                                                                         *
 *   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 "StatistikPage.h"

#include <tdepopupmenu.h>
#include <tdelocale.h>
#include <kdebug.h>

#include <kvoctraindoc.h>
#include <prefs.h>

#include <tqcursor.h>
#include <tqpainter.h>

#define MIN_COL_WIDTH      2
#define PIX_SHIFT          2
#define SIZE_GRADE         100+PIX_SHIFT
#define SIZE_COUNT         70
#define SIZE_LESSON        300

#define TB_FGRADE          0
#define TB_TGRADE          1
#define TB_COUNT           2
#define TB_LESSON          3


class GradeListItem : public TQListViewItem
{
public:
  inline GradeListItem (TQListView* parent, int _lesson): TQListViewItem(parent), lesson(_lesson) {}
  inline int getLesson() const { return lesson; }

private:
  int lesson;
};


StatistikPage::StatistikPage(int col, kvoctrainDoc  *_doc, TQWidget *parent, const char *name)
  : StatistikPageForm( parent, name ), doc(_doc)
{
  StatListView->setColumnWidth(0, SIZE_GRADE + 10);
  StatListView->setColumnWidth(1, SIZE_GRADE + 10);
  StatListView->setColumnWidth(2, SIZE_COUNT);
  StatListView->setColumnWidth(3, SIZE_LESSON);

  vector<TQString> lesson = doc->getLessonDescr();

  fsc.resize(lesson.size()+1);
  tsc.resize(lesson.size()+1);

  // accumulate numbers of grades per lesson
  for (int i = 0; i < (int) doc->numEntries(); i++) {
    kvoctrainExpr *expr = doc->getEntry(i);
    int fg = TQMIN(KV_MAX_GRADE, expr->getGrade(col, false));
    int tg = TQMIN(KV_MAX_GRADE, expr->getGrade(col, true));
    int l = expr->getLesson();
    if (l >= 0 && l <= (int) lesson.size() ) {
      fsc[l].grade[fg]++;
      fsc[l].num++;
      tsc[l].grade[tg]++;
      tsc[l].num++;
    }
  }
  setupPixmaps();
  connect(StatListView, TQT_SIGNAL( rightButtonPressed( TQListViewItem *, const TQPoint& , int ) ),
          this, TQT_SLOT( slotRMB( TQListViewItem *, const TQPoint &, int ) ) );
}


void StatistikPage::setupPixmaps()
{
  // create pixmaps with bar charts of numbers of grades
  int height;
  GradeListItem lvi (StatListView, 0);
  height = lvi.height();
  for (int entry = 0; entry < (int) fsc.size(); entry++) {
    TQPainter p;
    TQColor color;
    TQPixmap fpix (SIZE_GRADE, height);
    p.begin( &fpix);
    p.eraseRect (0, 0, fpix.width(), fpix.height());
    p.setPen( black );
    if (fsc[entry].num != 0) {

      int num = 0;
      for (int j = KV_NORM_GRADE; j <= KV_MAX_GRADE; j++)
        if (fsc[entry].grade[j] != 0)
          num++;

      int maxw = fpix.width() -PIX_SHIFT -PIX_SHIFT -1;
      int w = maxw;
      int widths [KV_MAX_GRADE+1];
      for (int j = KV_NORM_GRADE; j <= KV_MAX_GRADE; j++) {
        if (fsc[entry].grade[j] == 0)
          widths[j] = 0;
        else {
          if (num <= 1) {
            widths[j] = w;
            w = 0;
          }
          else {
            --num;
            widths[j] = TQMAX(MIN_COL_WIDTH, fsc[entry].grade[j] * maxw / fsc[entry].num);
            w -= widths[j];
          }
        }
      }

      int x = 0;
      int x2 = 1+PIX_SHIFT;
      for (int j = KV_MIN_GRADE; j <= KV_MAX_GRADE; j++) {
        switch (j) {
          case KV_NORM_GRADE: color = Prefs::gradeCol(0);    break;
          case KV_LEV1_GRADE: color = Prefs::gradeCol(1);    break;
          case KV_LEV2_GRADE: color = Prefs::gradeCol(2);    break;
          case KV_LEV3_GRADE: color = Prefs::gradeCol(3);    break;
          case KV_LEV4_GRADE: color = Prefs::gradeCol(4);    break;
          case KV_LEV5_GRADE: color = Prefs::gradeCol(5);    break;
          case KV_LEV6_GRADE: color = Prefs::gradeCol(6);    break;
          case KV_LEV7_GRADE: color = Prefs::gradeCol(7);    break;
          default           : color = Prefs::gradeCol(1);
        }
        if (widths[j] != 0) {
          x2 += widths[j];
          p.fillRect(x+PIX_SHIFT, 1, x2-x, height-1, color);
          p.drawRect(x+PIX_SHIFT, 1, x2-x, height-1);
          x = x2-1;
        }
      }
    }
    else {
      p.fillRect(PIX_SHIFT, 1, fpix.width()-PIX_SHIFT, height-1, Prefs::gradeCol(0));
      p.drawRect(PIX_SHIFT, 1, fpix.width()-PIX_SHIFT, height-1);
    }
    p.end();
    from_pix.push_back(fpix);

    TQPixmap tpix (SIZE_GRADE, height);
    p.begin( &tpix );
    p.eraseRect (0, 0, tpix.width(), tpix.height());
    p.setPen( black );
    if (tsc[entry].num != 0) {
      int num = 0;
      for (int j = KV_NORM_GRADE; j <= KV_MAX_GRADE; j++)
        if (tsc[entry].grade[j] != 0)
          num++;

      int maxw = tpix.width() -PIX_SHIFT -PIX_SHIFT -1;
      int w = maxw;
      int widths [KV_MAX_GRADE+1];
      for (int j = KV_NORM_GRADE; j <= KV_MAX_GRADE; j++) {
        if (tsc[entry].grade[j] == 0)
          widths[j] = 0;
        else {
          if (num <= 1) {
            widths[j] = w;
            w = 0;
          }
          else {
            --num;
            widths[j] = TQMAX(MIN_COL_WIDTH, tsc[entry].grade[j] * maxw / tsc[entry].num);
            w -= widths[j];
          }
        }
      }

      int x = 0;
      int x2 = 1+PIX_SHIFT;
      for (int j = KV_MIN_GRADE; j <= KV_MAX_GRADE; j++) {
        switch (j) {
          case KV_NORM_GRADE: color = Prefs::gradeCol(0);    break;
          case KV_LEV1_GRADE: color = Prefs::gradeCol(1);    break;
          case KV_LEV2_GRADE: color = Prefs::gradeCol(2);    break;
          case KV_LEV3_GRADE: color = Prefs::gradeCol(3);    break;
          case KV_LEV4_GRADE: color = Prefs::gradeCol(4);    break;
          case KV_LEV5_GRADE: color = Prefs::gradeCol(5);    break;
          case KV_LEV6_GRADE: color = Prefs::gradeCol(6);    break;
          case KV_LEV7_GRADE: color = Prefs::gradeCol(7);    break;
          default           : color = Prefs::gradeCol(1);
        }
        if (widths[j] != 0) {
          x2 += widths[j];
          p.fillRect(x+PIX_SHIFT, 1, x2-x, height-1, color);
          p.drawRect(x+PIX_SHIFT, 1, x2-x, height-1);
          x = x2-1;
        }
      }
    }
    else {
      p.fillRect(PIX_SHIFT, 1, tpix.width()-PIX_SHIFT, height-1, Prefs::gradeCol(0));
      p.drawRect(PIX_SHIFT, 1, tpix.width()-PIX_SHIFT, height-1);
    }
    p.end();
    to_pix.push_back(tpix);
  }

  // setup rows with pixmaps and strings
  vector<TQString> lesson = doc->getLessonDescr();

  TQString s;

  GradeListItem *plvi = new GradeListItem(StatListView, 0);
  plvi->setPixmap (TB_FGRADE, from_pix[0]);
  plvi->setPixmap (TB_TGRADE, to_pix[0]);
  s.setNum (tsc[0].num);
  plvi->setText (TB_COUNT, s);
  plvi->setText (TB_LESSON, i18n("<no lesson>"));
  StatListView->insertItem (plvi);

  for (int i = 0; i < (int) lesson.size(); i++) {
    plvi = new GradeListItem(StatListView, i+1);
    plvi->setPixmap (TB_FGRADE, from_pix[i+1]);
    plvi->setPixmap (TB_TGRADE, to_pix[i+1]);
    s.setNum (tsc[i+1].num);
    plvi->setText (TB_COUNT, s);
    plvi->setText (TB_LESSON, lesson[i]);
    StatListView->insertItem (plvi);
  }
}


void StatistikPage::slotRMB( TQListViewItem* Item, const TQPoint & /*point*/, int col)
{
  if( Item != 0)
    slotPopupMenu(((GradeListItem*)Item)->getLesson(), col);
}


void StatistikPage::slotPopupMenu(int row, int col)
{
  struct stat_counter *sc;

  if (col == TB_FGRADE) {
    if (row >= (int) fsc.size() ) {
      kdError() << "row >= fsc.size()" << endl;
      return;
    }
    else
      sc = &fsc[row];
  }
  else if (col == TB_TGRADE) {
    if (row >= (int) tsc.size() ) {
      kdError() << "row >= tsc.size()" << endl;
      return;
    }
    else
      sc = &tsc[row];
  }
  else
    return;

  TDEPopupMenu *header_m = new TDEPopupMenu(i18n("Number of Entries per Grade"));

  header_m->insertItem (i18n(KV_NORM_TEXT) + "\t" + TQString::number(sc->grade[KV_NORM_GRADE]) );
  header_m->insertItem (i18n(KV_LEV1_TEXT) + "\t" + TQString::number(sc->grade[KV_LEV1_GRADE]) );
  header_m->insertItem (i18n(KV_LEV2_TEXT) + "\t" + TQString::number(sc->grade[KV_LEV2_GRADE]) );
  header_m->insertItem (i18n(KV_LEV3_TEXT) + "\t" + TQString::number(sc->grade[KV_LEV3_GRADE]) );
  header_m->insertItem (i18n(KV_LEV4_TEXT) + "\t" + TQString::number(sc->grade[KV_LEV4_GRADE]) );
  header_m->insertItem (i18n(KV_LEV5_TEXT) + "\t" + TQString::number(sc->grade[KV_LEV5_GRADE]) );
  header_m->insertItem (i18n(KV_LEV6_TEXT) + "\t" + TQString::number(sc->grade[KV_LEV6_GRADE]) );
  header_m->insertItem (i18n(KV_LEV7_TEXT) + "\t" + TQString::number(sc->grade[KV_LEV7_GRADE]) );

  header_m->exec(TQCursor::pos()+TQPoint(10, 0));
}

#include "StatistikPage.moc"