summaryrefslogtreecommitdiffstats
path: root/kvoctrain/kvoctrain/kvoctraintableitem.cpp
blob: 9972cd6dfd0735ed09139560a315ed45a2b90a1f (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
/***************************************************************************

                      table item for the main view

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

    begin                : Mon Dec 27 17:05:53 PST 2004

    copyright            : (C) 2004-2005 Peter Hedlund

    email                : 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 <qlineedit.h>
#include <qcombobox.h>

#include <klocale.h>
#include <kglobalsettings.h>

#include "kvoctraintableitem.h"


KVocTrainTableItem::KVocTrainTableItem( QTable *t, EditType et, kvoctrainDoc *doc)
  : QTableItem( t, et, QString::null )
{
  m_doc = doc;
  // we do not want that this item can be replaced
  setReplaceable(false);
}


void KVocTrainTableItem::setPosition(int curr_row, int curr_col)
{
  setRow(curr_row);
  setCol(curr_col);
}


void KVocTrainTableItem::setDoc(kvoctrainDoc *doc)
{
  m_doc = doc;
}


QWidget *KVocTrainTableItem::createEditor() const
{
  if (m_doc != 0 && m_doc->numEntries() != 0 && row() >= 0 && col() >= 0) {
    switch (col()) {
      case KV_COL_LESS: {
        QComboBox *lessonbox = new QComboBox(table()->viewport() );
        lessonbox->setFont(KGlobalSettings::generalFont());
        lessonbox->insertItem (m_doc->getLessonDescr(0));
        for (unsigned i = 1; i <= (unsigned) m_doc->numLessons(); ++i)
          lessonbox->insertItem (m_doc->getLessonDescr(i));
        lessonbox->setCurrentItem(m_doc->getEntry(row())->getLesson());
        return lessonbox;
      }
      break;

      case KV_COL_MARK: {
        QComboBox *statebox = new QComboBox(table()->viewport() );
        statebox->setFont(KGlobalSettings::generalFont());
        statebox->insertItem (i18n("state of a row", "Active, Not in Query"));
        statebox->insertItem (i18n("state of a row", "In Query"));
        statebox->insertItem (i18n("state of a row", "Inactive"));
        QSize sz = statebox->sizeHint();
        sz.setHeight(table()->rowHeight(row()));
        statebox->setMinimumSize(sz);
        if (!m_doc->getEntry(row())->isActive() )
          statebox->setCurrentItem(2);
        else if (m_doc->getEntry(row())->isInQuery() )
          statebox->setCurrentItem(1);
        else
          statebox->setCurrentItem(0);
        return statebox;
      }
      break;

      default: {
        QLineEdit *edit = new QLineEdit(table()->viewport() );
        edit->setFrame(false);
        if (col() == KV_COL_ORG)
          edit->setText(m_doc->getEntry(row())->getOriginal());
        else
          edit->setText(m_doc->getEntry(row())->getTranslation(col()-KV_COL_ORG));
        return edit;
      }
    }
  }
  return 0;
}


void KVocTrainTableItem::setContentFromEditor( QWidget *w )
{
  if (m_doc != 0) {
    if ( w->inherits( "QComboBox" ) ) {
      if (col() == KV_COL_MARK) {
        QComboBox *statebox = (QComboBox*) w;
        kvoctrainExpr *expr = m_doc->getEntry(row());
        bool inq = false;
        bool act = true;
        if (statebox->currentItem() == 0) {
          inq = false;
          act = true;
        }
        else if (statebox->currentItem() == 1) {
          inq = true;
          act = true;
        }
        else if (statebox->currentItem() == 2) {
          inq = false;
          act = false;
        }
        if (inq != expr->isInQuery() ||
            act != expr->isActive() )
          m_doc->setModified();
        expr->setInQuery(inq);
        expr->setActive(act);
      }
      else if (col() == KV_COL_LESS) {
        QComboBox *lessonbox = (QComboBox*) w;
        if (m_doc->getEntry(row())->getLesson() != lessonbox->currentItem())
          m_doc->setModified();
        m_doc->getEntry(row())->setLesson(lessonbox->currentItem());
      }
    }
    else {
      QLineEdit *edit = (QLineEdit*) w;
      if (col() == KV_COL_ORG) {
        if (m_doc->getEntry(row())->getOriginal() != edit->text())
          m_doc->setModified();
        m_doc->getEntry(row())->setOriginal(edit->text());
      }
      else {
        if (m_doc->getEntry(row())->getTranslation(col()-KV_COL_ORG) != edit->text())
          m_doc->setModified();
        m_doc->getEntry(row())->setTranslation(col()-KV_COL_ORG, (edit->text()));
      }
    }
  }
}