[r73]: trunk / sqlitebrowser / sqlitebrowser / editform.cpp Maximize Restore History

Download this file

editform.cpp    253 lines (211 with data), 5.7 kB

  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
#include "editform.h"
#include <qvariant.h>
#include <q3filedialog.h>
#include <qmessagebox.h>
#include <qimage.h>
#include <qpixmap.h>
#include <Q3TextStream>
#include "sqlitedb.h"
/*
* Constructs a editForm as a child of 'parent', with the
* name 'name' and widget flags set to 'f'.
*
* The dialog will by default be modeless, unless you set 'modal' to
* true to construct a modal dialog.
*/
editForm::editForm(QWidget* parent, const char* name, bool modal, Qt::WindowFlags fl)
: QDialog(parent, name, modal, fl)
{
setupUi(this);
init();
}
/*
* Destroys the object and frees any allocated resources
*/
editForm::~editForm()
{
destroy();
// no need to delete child widgets, Qt does it all for us
}
/*
* Sets the strings of the subwidgets using the current
* language.
*/
void editForm::languageChange()
{
retranslateUi(this);
}
void editForm::init()
{
setModified(false);
dataType = kSQLiteMediaType_Void;
dataSize = 0;
dataDepth = 0;
curRow = -1;
curCol = -1;
textEditor->setText("");
setDataType(kSQLiteMediaType_Void, 0);
}
void editForm::destroy()
{
}
void editForm::reset()
{
init();
}
void editForm::setModified(bool modifiedstate)
{
modified = modifiedstate;
saveChangesButton->setEnabled(modified);
// set the save button to the default (unless they explicitly cancel this is the most likely operation)
// if the save button isn't enabled we want the cancel button to be the default - can't save if there are no changes!
saveChangesButton->setDefault(modified);
closeButton->setDefault(!modified);
}
void editForm::enableExport(bool enabled)
{
exportButton->setEnabled(enabled);
}
void editForm::enableTextEditor(bool enabled)
{
textEditor->setReadOnly(!enabled);
}
void editForm::setTextFormat(QString format)
{
if (format=="Auto")
{
textEditor->setTextFormat(Qt::AutoText);
} else {
textEditor->setTextFormat(Qt::PlainText);
}
}
void editForm::setDataType(int type, int size)
{
dataType = type;
dataSize = size;
QString charstr;
switch (dataType ) {
case kSQLiteMediaType_String:
currentTypeLabel->setText("Type of data currently in cell: Text / Numeric");
if (textEditor->length()>1) charstr = QString("chars"); else charstr = QString("char");
currentDataInfo->setText(QString("%1 %2").arg(textEditor->length()).arg(charstr));
editWidgetStack->setCurrentIndex(0);
enableExport(true);
enableTextEditor(true);
break;
case kSQLiteMediaType_Void:
currentTypeLabel->setText("Type of data currently in cell: Empty");
currentDataInfo->setText("");
editWidgetStack->setCurrentIndex(0);
enableExport(false);
enableTextEditor(true);
break;
}
}
void editForm::closeEvent( QCloseEvent * )
{
emit goingAway();
}
void editForm::loadText(QString text, int row, int col)
{
textEditor->setText(text);
// select the editor and the contents of the cell so it is ready to be edited immediately
textEditor->selectAll(true);
curRow = row;
curCol = col;
if (textEditor->length() > 0)
{
setDataType(kSQLiteMediaType_String, 0);
} else {
setDataType(kSQLiteMediaType_Void, 0);
}
setModified(false);
}
void editForm::importData()
{
int type = kSQLiteMediaType_Void;
QString fileName = Q3FileDialog::getOpenFileName(
"",
QString( "Text files (*.txt);;"
"All files (*.*);;"),
this,
"open file dialog"
"Choose an image file" );
if (QFile::exists(fileName) )
{
type = kSQLiteMediaType_String;
QFile file( fileName );
if ( file.open( QIODevice::ReadOnly ) ) {
Q3TextStream stream( &file );
textEditor->setText(stream.read());
file.close();
}
setModified(true);
}
}
void editForm::exportData()
{
QString filter;
switch (dataType)
{
case kSQLiteMediaType_String:
filter = "Text files (*.txt)";
break;
default:
return;
}
QString fileName = Q3FileDialog::getSaveFileName(
defaultlocation,
filter,
this,
"save file dialog"
"Choose a filename to export data" );
if (fileName.size() > 0)
{
switch (dataType)
{
case kSQLiteMediaType_String:
{
QFile file(fileName);
if ( file.open( QIODevice::WriteOnly ) ) {
Q3TextStream stream( &file );
stream << textEditor->text();
file.close();
}
}
break;
default:
return;
}
}
}
void editForm::clearData()
{
textEditor->setText("");
setDataType(kSQLiteMediaType_Void, 0);
setModified(true);
}
void editForm::saveChanges()
{
if (dataType==kSQLiteMediaType_String)
emit updateRecordText(curRow, curCol, textEditor->text());
if (dataType==kSQLiteMediaType_Void)
emit updateRecordText(curRow, curCol, QString(""));
setModified(false);
emit goingAway();
}
void editForm::editTextChanged()
{
int newtype = kSQLiteMediaType_String;
if (textEditor->length() == 0){
newtype = kSQLiteMediaType_Void;
}
if (newtype!=dataType){
setDataType(newtype, 0);
}
setModified(true);
}
void editForm::editorClicked()
{
enableTextEditor(true);
}