summaryrefslogtreecommitdiffstats
path: root/ktouch/src/ktouchkeyboardeditor.cpp
blob: 579f8a694e746a252593595f67b76e0362012efe (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
/***************************************************************************
 *   ktouchkeyboardeditor.cpp                                              *
 *   ------------------------                                              *
 *   Copyright (C) 2000 by Håvard Frøiland, 2004 by Andreas Nicolai        *
 *   [email protected], [email protected]          *
 *                                                                         *
 *   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 "ktouchkeyboardeditor.h"
#include "ktouchkeyboardeditor.moc"

#include <tqlabel.h>
#include <tqfont.h>

#include <tdemessagebox.h>
#include <tdefiledialog.h>
#include <tdelocale.h>
#include <ksqueezedtextlabel.h>
#include <klineedit.h>
#include <ktextedit.h>
#include <tdefontdialog.h>
#include <kdebug.h>

#include <utility>

#include "ktouch.h"
#include "ktouchopenrequest.h"

// **************************
// ***** Public functions ***
// **************************

KTouchKeyboardEditor::KTouchKeyboardEditor(TQWidget* parent, const char* name, bool modal, WFlags fl)
  : KTouchKeyboardEditorDlg(parent,name, modal,fl)
{
}
// -----------------------------------------------------------------------------

bool KTouchKeyboardEditor::startEditor(const KURL& url) {
    // call open request dialog and load a keyboard and start the dialogs event loop if
    // the user did not cancel the open request dialog 
    if (openKeyboardFile(url)==TQDialog::Accepted)  {
        exec();
        return true;
    }
    else  return false;
}
// -----------------------------------------------------------------------------


// ************************
// ***** Public slots *****
// ************************


void KTouchKeyboardEditor::fontBtnClicked() {
	//kdDebug() << "Fontbutton clicked" << endl;
    TQFont f;
    if (TDEFontDialog::getFont(f, false, this, true)==TQDialog::Accepted) {
		m_keyboard.m_fontSuggestions = f.toString();
		// update font and keyboard display
		titleEdit->setFont(f);  
		keyboardCommentEdit->setFont(f);  
		languageEdit->setFont(f);  
		update();	// trigger repaint of the keyboard.
		setModified();
    }
}
// -----------------------------------------------------------------------------

void KTouchKeyboardEditor::openBtnClicked() {
    saveModified();  // save if modified
    openKeyboardFile("");
}
// -----------------------------------------------------------------------------

void KTouchKeyboardEditor::saveBtnClicked() {
    if (m_currentURL.isEmpty()) saveAsBtnClicked();
    else {
        transfer_from_dialog();
        //m_keyboard.saveXML(this, m_currentURL);
        setModified(false);
    }
}
// -----------------------------------------------------------------------------

void KTouchKeyboardEditor::saveAsBtnClicked() {
    TQString tmp = KFileDialog::getSaveFileName(TQString(), 
        i18n("*.keyboard.xml|KTouch Keyboard Files (*.keyboard.xml)\n*.*|All Files"), this, i18n("Save Keyboard Layout") );
    if (!tmp.isEmpty()) {
        transfer_from_dialog();
        m_currentURL = tmp;
        //m_keyboard.saveXML(this, m_currentURL);
        setModified(false);
    }
}
// -----------------------------------------------------------------------------

/// Called when the "Add..." button was clicked
void KTouchKeyboardEditor::addBtnClicked() {
}
// -----------------------------------------------------------------------------

/// Called when the "Edit..." button was clicked
void KTouchKeyboardEditor::editBtnClicked() {
}
// -----------------------------------------------------------------------------

/// Called when the "Remove" button was clicked
void KTouchKeyboardEditor::removeBtnClicked() {
}
// -----------------------------------------------------------------------------

/// Called when the selection in the key list box has changed
void KTouchKeyboardEditor::keySelectionChanged(TQListBoxItem * item) {
}
// -----------------------------------------------------------------------------



void KTouchKeyboardEditor::paintEvent(TQPaintEvent *) {
	
}
// -----------------------------------------------------------------------------

// ****************************
// ***** Private functions ****
// ****************************

void KTouchKeyboardEditor::transfer_to_dialog() {
	if (m_currentURL.isEmpty()) {
		titleEdit->setText( i18n("untitled keyboard layout") );
		keyboardCommentEdit->setText("");
	}
	else {
		titleEdit->setText(m_keyboard.m_title);
		keyboardCommentEdit->setText(m_keyboard.m_comment);
	}
	languageEdit->setText(m_keyboard.m_language);
	kdDebug() << "Setting font '"<< m_keyboard.m_fontSuggestions <<"'" << endl;
	if (!m_keyboard.m_fontSuggestions.isEmpty()) {
		TQFont f;
		f.fromString(m_keyboard.m_fontSuggestions);
		titleEdit->setFont(f);  
		keyboardCommentEdit->setFont(f);
		languageEdit->setFont(f);
	}
	kdDebug() << "Adding key definitions to key list" << endl;
	keyListBox->clear();
	TQValueVector<KTouchKey>::iterator it;
	unsigned int min_x = 100000;
	unsigned int max_x = 0;
	unsigned int min_y = 100000;
	unsigned int max_y = 0;
	for( it = m_keyboard.m_keys.begin(); it != m_keyboard.m_keys.end(); ++it ) {
		switch (it->m_type) {
			case KTouchKey::NORMAL : keyListBox->insertItem("N  '" + TQString(it->m_primaryChar) + "'"); break;
			case KTouchKey::FINGER : keyListBox->insertItem("F  '" + TQString(it->m_primaryChar) + "'"); break;
			default                : keyListBox->insertItem("O  '" + it->m_otherKeyText + "'"); break;
		}
		min_x = std::min<unsigned int>(min_x, it->m_x);
		max_x = std::max<unsigned int>(max_x, it->m_x+it->m_w);
		min_y = std::min<unsigned int>(min_y, it->m_y);
		max_y = std::max<unsigned int>(max_y, it->m_y+it->m_h);
	}
	dimensionsLabel->setText( i18n("Keyboard dimensions: %1 x %2").arg(max_x - min_x).arg(max_y - min_y) );
}
// -----------------------------------------------------------------------------
    
void KTouchKeyboardEditor::transfer_from_dialog() {
	m_keyboard.m_title = titleEdit->text();
	if (m_keyboard.m_title.isEmpty())  m_keyboard.m_title = i18n("untitled keyboard layout");
	m_keyboard.m_comment = keyboardCommentEdit->text();
	m_keyboard.m_language = languageEdit->text();
}
// -----------------------------------------------------------------------------

int KTouchKeyboardEditor::openKeyboardFile(const KURL& url) {
    // First setup the open request dialog
    KTouchOpenRequest dlg(this);
    // Call the dialog
    KURL new_url;
    int result = dlg.requestFileToOpen(new_url,
        i18n("Open keyboard file ..."),
        i18n("Which keyboard file would you like to edit?"),
        i18n("Edit current keyboard:"),
        i18n("Open a default keyboard:"),
        i18n("Open a keyboard file:"),
        i18n("Create new keyboard!"),
        url, KTouchPtr->lectureFiles(), i18n("<no keyboard files available>"));

    if (result == TQDialog::Accepted) {
        // Ok, user confirmed the dialog, now lets get the url
        m_currentURL = new_url;
        // Try to load the keyboard, if that fails, we create a new keyboard instead
        if (!m_currentURL.isEmpty() && 
		    !m_keyboard.load(this, m_currentURL) && !m_keyboard.loadXML(this, m_currentURL)) 
		{
            KMessageBox::sorry(this, i18n("Could not open the keyboard file, creating a new one instead!"));
            m_currentURL = TQString(); // new keyboards haven't got a URL
			m_keyboard.clear();
        }
        // If we have no URL, we create a new keyboard - can happen if either the user
        // chose "new keyboard" or the chosen keyboard could not be opened
        if (m_currentURL.isEmpty())  {
            m_keyboard.clear();
            setModified(true); // new keyboards are modified by default
        }
        else
            setModified(false); // newly read keyboards are not modified in the begin
        transfer_to_dialog();    // Update our editor with the keyboard data
        return TQDialog::Accepted;
    }
    else return TQDialog::Rejected;
}
// -----------------------------------------------------------------------------

void KTouchKeyboardEditor::setModified(bool flag) {
    m_modified = flag;
    if (!m_currentURL.isEmpty()) {
        if (flag) this->setCaption("KTouch Keyboard Editor - " + m_currentURL.url() + i18n(" (modified)"));
        else      this->setCaption("KTouch Keyboard Editor - " + m_currentURL.url());
    }
	else
		this->setCaption("KTouch Keyboard Editor - " + i18n("<unnamed keyboard file>"));
}
// -----------------------------------------------------------------------------

bool KTouchKeyboardEditor::saveModified() {
    if (!m_modified) return true;
    // ok, ask the user to save the changes
    int result = KMessageBox::questionYesNoCancel(this, 
        i18n("The keyboard has been changed. Do you want to save the changes?"),TQString(),KStdGuiItem::save(),KStdGuiItem::discard());
    if (result == KMessageBox::Cancel) return false; // User aborted
    if (result == KMessageBox::Yes) saveBtnClicked();
    // if successfully saved the modified flag will be resetted in the saveBtnClicked() function
    return true; // User acknowledged
}
// -----------------------------------------------------------------------------