summaryrefslogtreecommitdiffstats
path: root/kanagram/src/vocabedit.cpp
blob: df63da27382e055cf3645612fd7e970d52241de3 (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
/***************************************************************************
 *   Copyright (C) 2005 by Joshua Keel                                     *
 *   joshuakeel@gmail.com                                                  *
 *                                                                         *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.             *
 ***************************************************************************/

#include "vocabedit.h"

#include <tqpushbutton.h>
#include <tqlistbox.h>
#include <tqlineedit.h>
#include <tqfile.h>
#include <tqstring.h>
#include <tqvaluevector.h>

#include <kstandarddirs.h>
#include <kglobal.h>
#include <kurl.h>
#include <kdebug.h>
#include <kmessagebox.h>
#include <klocale.h>

#include "keduvocdocument.h"
#include "keduvocexpression.h"
#include "vocabsettings.h"
#include "kanagramsettings.h"


VocabEdit::VocabEdit(TQWidget *tqparent, TQString fileName) : VocabEditWidget(tqparent), m_fileName("")
{
	if(fileName != "")
	{
		m_fileName = fileName;
		KEduVocDocument	*doc = new KEduVocDocument(TQT_TQOBJECT(this));
		doc->open(KURL(m_fileName), false);
		for(int i = 0; i < doc->numEntries(); i++)
		{
			KEduVocExpression expr = *doc->getEntry(i);
			m_vocabList.append(expr);
			lboxWords->insertItem(doc->getEntry(i)->getOriginal());	
		}
		txtVocabName->setText(doc->getTitle());
		txtDescription->setText(doc->getDocRemark());
	}

	connect(btnSave, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotSave()));
	connect(btnNewWord, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotNewWord()));
	connect(btnRemoveWord, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotRemoveWord()));
	connect(btnClose, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotClose()));
	
	connect(txtWord, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(slotWordTextChanged(const TQString &)));
	connect(txtHint, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(slotHintTextChanged(const TQString &)));

	//Connect the name and description boxes to a general textChanged slot, so that we can keep track of
	//whether they've been changed or not
	connect(txtVocabName, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(slotTextChanged(const TQString &)));
	connect(txtDescription, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(slotTextChanged(const TQString &)));

	connect(lboxWords, TQT_SIGNAL(selectionChanged()), this, TQT_SLOT(slotSelectionChanged()));

	//Has anything in the dialog changed?
	m_textChanged = false;
}

VocabEdit::~VocabEdit()
{
}

void VocabEdit::slotSave()
{
	KEduVocDocument *doc = new KEduVocDocument(TQT_TQOBJECT(this));
	doc->setTitle(txtVocabName->text());
	doc->setDocRemark(txtDescription->text());
	for(uint i = 0; i < m_vocabList.size(); i++)
	{
		doc->appendEntry(&m_vocabList[i]);
	}
	
	TQString fileName;
	if(m_fileName == "")
	{
		fileName = KGlobal::dirs()->saveLocation("data", "kanagram/data/" + KanagramSettings::dataLanguage()) + txtVocabName->text().lower().replace(" ", "") + ".kvtml";
	}
	else
	{
		fileName = m_fileName;
	}
	doc->saveAs(TQT_TQOBJECT(this), KURL(fileName), KEduVocDocument::automatic, "kanagram");

	VocabSettings *settings = (VocabSettings*)this->parentWidget();
	settings->refreshView();

	if(m_textChanged)
		m_textChanged = false;
}

void VocabEdit::slotClose()
{
	//Has anything in the dialog changed?
	if(m_textChanged && lboxWords->count() > 0)
	{
		int code = KMessageBox::warningYesNo(this, i18n("Would you like to save your changes?"), i18n("Save Changes Dialog"));
		if(code == KMessageBox::Yes)
		{
			slotSave();
			close();
		}
		else
			close();
	}
	else
		close();
}

void VocabEdit::slotNewWord()
{
	lboxWords->insertItem("New Item");
	KEduVocExpression expr = KEduVocExpression();
	m_vocabList.append(expr);

	if(m_textChanged == false)
		m_textChanged = true;
}

void VocabEdit::slotSelectionChanged()
{
	//A little hack to make things work right
	disconnect(txtWord, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(slotWordTextChanged(const TQString &)));
	disconnect(txtHint, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(slotHintTextChanged(const TQString &)));
	if(lboxWords->currentItem() >= 0)
	{
		txtWord->setText(m_vocabList[lboxWords->currentItem()].getOriginal());
		txtHint->setText(m_vocabList[lboxWords->currentItem()].getRemark(0));
	}
	connect(txtWord, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(slotWordTextChanged(const TQString &)));
	connect(txtHint, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(slotHintTextChanged(const TQString &)));
}

void VocabEdit::slotWordTextChanged(const TQString &changes)
{
	//Make sure there actually is a currentItem()
	if(lboxWords->currentItem() != -1)
	{
		m_vocabList[lboxWords->currentItem()].setOriginal(changes);
		lboxWords->changeItem(changes, lboxWords->currentItem());
	}

	if(m_textChanged == false)
		m_textChanged = true;
}

void VocabEdit::slotHintTextChanged(const TQString &changes)
{
	//Make sure there actually is a currentItem()
	if(lboxWords->currentItem() != -1)
		m_vocabList[lboxWords->currentItem()].setRemark(0, changes);

	if(m_textChanged == false)
		m_textChanged = true;
}

void VocabEdit::slotTextChanged(const TQString &changes)
{
	//Make sure we know when text has been modified and not saved, so we
	//can notify the user
	if(m_textChanged == false)
		m_textChanged = true;

	//Make gcc happy
	(void)changes;
}

void VocabEdit::slotRemoveWord()
{
	if (lboxWords->count()) {
		m_vocabList.erase(m_vocabList.begin() + lboxWords->currentItem());
		lboxWords->removeItem(lboxWords->currentItem());
	}

	if(m_textChanged == false)
		m_textChanged = true;
}

#include "vocabedit.moc"