summaryrefslogtreecommitdiffstats
path: root/src/kile/userhelpdialog.cpp
blob: 80c7fa884f99aa0bb3c64416668fb97faacf764c (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/***************************************************************************
                           userhelpdialog.cpp
----------------------------------------------------------------------------
    date                 : Jul 22 2005
    version              : 0.20
    copyright            : (C) 2005 by Holger Danielsson
    email                : holger.danielsson@t-online.de
 ***************************************************************************/

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

#include <tqlayout.h>
#include <tqvgroupbox.h>
#include <tqlabel.h>
#include <tqinputdialog.h>
#include <tqfileinfo.h>
#include <tqwhatsthis.h>

#include <klocale.h>
#include <kfiledialog.h>
#include <kiconloader.h>
#include <kmessagebox.h>
#include <krun.h>
#include <kurl.h>
#include "kiledebug.h"


namespace KileDialog
{

//////////////////// UserHelpDialog ////////////////////

//BEGIN UserHelpDialog

UserHelpDialog::UserHelpDialog(TQWidget *tqparent, const char *name)
	: KDialogBase( tqparent, name, true, i18n("Configure User Help"), Cancel | Ok, Ok, true )
{
	KILE_DEBUG() << "==UserHelpDialog::UserHelpDialog()===================" << endl;

	TQWidget *page = new TQWidget(this);
	setMainWidget(page);

	// tqlayout
	TQVBoxLayout *vbox = new TQVBoxLayout(page, 6,6 );
	TQVGroupBox* group= new TQVGroupBox(i18n("User Help"),page );

	TQWidget *widget = new TQWidget(group);
	TQGridLayout *grid = new TQGridLayout( widget, 5,3, 5,5, "" );
	grid->setRowStretch(1,1);
	grid->setColStretch(0,1);
	grid->setRowSpacing(2,12);
	grid->setColSpacing(1,20);

	// listbox
	TQLabel *label1 = new TQLabel(i18n("&Menu item:"),widget);
	grid->addWidget( label1,0,0 );
	m_menulistbox = new KListBox(widget);
	grid->addWidget( m_menulistbox, 1,0 );
	label1->setBuddy(m_menulistbox);

	// action widget
	TQWidget *actionwidget = new TQWidget(widget);
	TQVBoxLayout *actions = new TQVBoxLayout(actionwidget);

	m_add = new KPushButton(i18n("&Add..."),actionwidget);
	m_remove = new KPushButton(i18n("&Remove"),actionwidget);
	m_addsep = new KPushButton(i18n("&Separator"),actionwidget);
	m_up = new KPushButton(i18n("Move &Up"),actionwidget);
	m_down = new KPushButton(i18n("Move &Down"),actionwidget);

	int wmax = m_add->tqsizeHint().width();
	int w = m_remove->tqsizeHint().width();
	if ( w > wmax ) wmax = w;
	w = m_addsep->tqsizeHint().width();
	if ( w > wmax ) wmax = w;
	w = m_up->tqsizeHint().width();
	if ( w > wmax ) wmax = w;
	w = m_down->tqsizeHint().width();
	if ( w > wmax ) wmax = w;

	m_add->setFixedWidth(wmax);
	m_remove->setFixedWidth(wmax);
	m_addsep->setFixedWidth(wmax);
	m_up->setFixedWidth(wmax);
	m_down->setFixedWidth(wmax);

	actions->addStretch(1);
	actions->addWidget(m_add);
	actions->addWidget(m_remove);
	actions->addSpacing(20);
	actions->addWidget(m_addsep);
	actions->addSpacing(20);
	actions->addWidget(m_up);
	actions->addWidget(m_down);
	actions->addStretch(1);

	// inserta ction widget
	grid->addWidget( actionwidget,1,2, TQt::AlignTop );

	// file
	TQLabel *label2 = new TQLabel(i18n("File:"),widget);
	grid->addWidget( label2, 3,0 );
	m_fileedit = new KLineEdit("",widget);
	m_fileedit->setReadOnly(true);
	grid->addMultiCellWidget( m_fileedit, 4,4,0,2 );

	// fill vbox
	vbox->addWidget(group);

	connect( m_menulistbox, TQT_SIGNAL(highlighted(int)),this,TQT_SLOT(slotChange(int)));
	connect( m_add, TQT_SIGNAL(clicked()), TQT_SLOT(slotAdd()) );
	connect( m_remove, TQT_SIGNAL(clicked()), TQT_SLOT(slotRemove()) );
	connect( m_addsep, TQT_SIGNAL(clicked()), TQT_SLOT(slotAddSep()) );
	connect( m_up, TQT_SIGNAL(clicked()), TQT_SLOT(slotUp()) );
	connect( m_down, TQT_SIGNAL(clicked()), TQT_SLOT(slotDown()) );

	resize(400,tqsizeHint().height());
	updateButton();
}

void UserHelpDialog::setParameter(const TQStringList &menuentries, const TQStringList &helpfiles)
{
	for (uint i=0; i<menuentries.count(); ++i)
	{
		m_menulistbox->insertItem(menuentries[i]);

		if ( m_menulistbox->text(i) != "-" )
			m_filelist << helpfiles[i];
		else
			m_filelist << TQString() ;
	}
	updateButton();
}

void UserHelpDialog::getParameter(TQStringList &userhelpmenulist, TQStringList &userhelpfilelist)
{
	// clear result
	userhelpmenulist.clear();
	userhelpfilelist.clear();
	bool separator = false;

	// now get all entries
	for (uint i=0; i<m_menulistbox->count(); ++i)
	{
		if ( m_menulistbox->text(i) != "-" )
		{
			userhelpmenulist << m_menulistbox->text(i);
			userhelpfilelist << m_filelist[i];
			separator = false;
		}
		else if ( !separator )
		{
			userhelpmenulist << m_menulistbox->text(i);
			userhelpfilelist << TQString();
			separator = true;
		}
	}
}

void UserHelpDialog::slotChange(int index)
{
	if ( index >= 0 )
	{
		m_fileedit->setText( m_filelist[index] );
	}
	else
	{
		m_fileedit->clear();
	}
	updateButton();
}

void UserHelpDialog::slotAdd()
{
	KileDialog::UserHelpAddDialog *dialog = new KileDialog::UserHelpAddDialog(m_menulistbox,this);
	if ( dialog->exec() )
	{
		// insert into listbox
		m_menulistbox->insertItem( dialog->getMenuitem() );
		m_menulistbox->setCurrentItem( m_menulistbox->count()-1 );

		// with corresponding filename
		TQString helpfile = dialog->getHelpfile();
		m_filelist.append(helpfile);
		m_fileedit->setText(helpfile);

		updateButton();
	}
}

void UserHelpDialog::slotRemove()
{
	// get current index
	int index = m_menulistbox->currentItem();
	if ( index >= 0 )
	{
		// remove item
		m_menulistbox->removeItem(index);
		m_filelist.remove( m_filelist.at(index) );

		// select a new index: first we try to take the old index. When
		// this index is too big now, index is decremented.
		// If the list is empty now, index is set to -1.
		int entries = (int)m_menulistbox->count();
		if ( entries > 0  )
		{
			if ( index >= entries )
				index--;
			m_menulistbox->setSelected(index,true);
		}
		else
		{
			m_menulistbox->setCurrentItem(-1);
		}
	}

	updateButton();
}

void UserHelpDialog::slotAddSep()
{
	// get current index
	int index = m_menulistbox->currentItem();
	if ( index == -1 ) return;

	// insert separator
	m_menulistbox->insertItem("-",index);
	m_filelist.insert( m_filelist.at(index) ,TQString() );

	updateButton();
}

void UserHelpDialog::slotUp()
{
	// get current index
	int index = m_menulistbox->currentItem();
	if ( index <= 0 ) return;

	// insert current entry before current
	m_menulistbox->insertItem(m_menulistbox->currentText(),index-1);
	m_filelist.insert( m_filelist.at(index-1) , m_filelist[index] );

	// then remove the old entry
	m_menulistbox->removeItem(index+1);
	m_filelist.remove( m_filelist.at(index+1) );

	// select current entry
	m_menulistbox->setSelected(index-1,true);

	updateButton();
}

void UserHelpDialog::slotDown()
{
	int entries = (int)m_menulistbox->count();

	// get current index
	int index = m_menulistbox->currentItem();
	if ( index<0 || index==entries-1 ) return;

	// insert current entry after current
	if ( index < entries-2 )
	{
		m_menulistbox->insertItem(m_menulistbox->currentText(),index+2);    // index + 2
		m_filelist.insert( m_filelist.at(index+2) , m_filelist[index] );
	}
	else
	{
		m_menulistbox->insertItem(m_menulistbox->currentText());            // at the end
		m_filelist.append( m_filelist[index] );
	}

	// then remove the old entry
	m_menulistbox->removeItem(index);
	m_filelist.remove( m_filelist.at(index) );

	// select current entry
	m_menulistbox->setSelected(index+1,true);

	updateButton();
}

void UserHelpDialog::updateButton()
{
	// default states
	bool rem_state = false;
	bool sep_state = false;
	bool up_state = false;
	bool down_state = false;

	// change button states, if there are entries
	int index = m_menulistbox->currentItem();
	int entries = (int)m_menulistbox->count();
	if ( entries == 1 )
	{
		rem_state = true;
	}
	else if ( entries >= 2 )
	{
		rem_state = true;
		if ( index == 0 )
		{
			down_state = true;         // index = 0
		}
		else if ( index == entries-1 )
		{
			sep_state = true;          // index = entries-1
			up_state = true;
		}
		else
		{                             // 0 < index < entries-1
			sep_state = true;
			up_state = true;
			down_state = true;
		}
	}

	// don't allow two continuous spearators
	if ( m_menulistbox->currentText() == "-" )
		sep_state = false;

	// set button states
	m_remove->setEnabled(rem_state);
	m_addsep->setEnabled(sep_state);
	m_up->setEnabled(up_state);
	m_down->setEnabled(down_state);
}
//END UserHelpDialog

//////////////////// UserHelpAddDialog ////////////////////

//BEGIN UserHelpAddDialog

UserHelpAddDialog::UserHelpAddDialog(KListBox *menulistbox, TQWidget *tqparent, const char *name)
	: KDialogBase( tqparent, name, true, i18n("Add User Helpfile"), Cancel | Ok, Ok, true ),
	  m_menulistbox(menulistbox)
{
	KILE_DEBUG() << "==UserHelpAddDialog::UserHelpAddDialog()===================" << endl;

	TQWidget *page = new TQWidget(this);
	setMainWidget(page);

	// tqlayout
	TQVBoxLayout *vbox = new TQVBoxLayout(page, 6,6 );
	TQVGroupBox* group= new TQVGroupBox(i18n("User Help"),page );

	TQWidget *widget = new TQWidget(group);
	TQGridLayout *grid = new TQGridLayout( widget, 2,6, 5,5, "" );
	grid->setColSpacing(2,8);
	grid->setColSpacing(4,8);

	// menu entry
	TQLabel *label1 = new TQLabel(i18n("&Menu entry:"),widget);
	grid->addWidget( label1,0,0 );
	m_leMenuEntry = new KLineEdit("",widget);
	grid->addWidget( m_leMenuEntry, 0,1 );
	label1->setBuddy(m_leMenuEntry);

	// help file
	TQLabel *label2 = new TQLabel(i18n("&Help file:"),widget);
	grid->addWidget( label2, 1,0 );
	m_leHelpFile = new KLineEdit("",widget);
	m_leHelpFile->setReadOnly(false);
	grid->addWidget( m_leHelpFile, 1,1 );
	m_pbChooseFile = new KPushButton("", widget, "filechooser_button" );
	m_pbChooseFile->setPixmap( SmallIcon("fileopen") );
	grid->addRowSpacing( 1, m_pbChooseFile->tqsizeHint().height()+5 );
	grid->addWidget(m_pbChooseFile,1,3);
	m_pbChooseHtml = new KPushButton("", widget, "htmlchooser_button" );
	m_pbChooseHtml->setPixmap( SmallIcon("viewhtml") );
	grid->addWidget(m_pbChooseHtml,1,5);
	grid->setColSpacing(3, m_pbChooseFile->tqsizeHint().width()+5 );
	grid->setColSpacing(5, m_pbChooseHtml->tqsizeHint().width()+5 );

	label2->setBuddy(m_pbChooseFile);

	// fill vbox
	vbox->addWidget(group);
	vbox->addStretch();

	TQWhatsThis::add(m_leMenuEntry,i18n("The menu entry for this help file."));
	TQWhatsThis::add(m_leHelpFile,i18n("The name of the local help file or a valid WEB url."));
	TQWhatsThis::add(m_pbChooseFile,i18n("Start a file dialog to choose a local help file."));
	TQWhatsThis::add(m_pbChooseHtml,i18n("Start the konqueror to choose a WEB url as help file. This url should be copied inzo the edit widget."));

	connect( m_pbChooseFile, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotChooseFile() ) );
	connect( m_pbChooseHtml, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotChooseHtml() ) );

	setFocusProxy( m_leMenuEntry );
	resize(500,tqsizeHint().height());
}

void UserHelpAddDialog::slotChooseFile()
{
	TQString directory = TQDir::currentDirPath();
	TQString filter = "*.*|All Files\n*.dvi|DVI Files\n*.ps|PS Files\n*.pdf|PDF Files\n*.html *.htm|HTML Files";

	TQString filename = KFileDialog::getOpenFileName( directory,filter,this,i18n("Select File") );
	if ( filename.isEmpty() )
		return;

	TQFileInfo fi(filename);
	if ( ! fi.exists() )
	{
		KMessageBox::error(0,TQString(i18n("File '%1' does not exist.")).tqarg(filename));
		return;
	}

	m_leHelpFile->setText( filename );
}

void UserHelpAddDialog::slotChooseHtml()
{
	KURL url;
	url.setPath("about:blank");
	KRun::runURL(url,"text/html");
}

void UserHelpAddDialog::slotOk()
{
	m_leMenuEntry->setText( m_leMenuEntry->text().stripWhiteSpace() );
	TQString filename = m_leHelpFile->text().stripWhiteSpace();
	m_leHelpFile->setText( filename );

	if ( m_leMenuEntry->text().isEmpty() )
	{
		KMessageBox::error(this,i18n("No menuitem was given."));
		return;
	}

	if ( m_menulistbox->findItem(m_leMenuEntry->text(),TQt::ExactMatch) )
	{
		KMessageBox::error(this,i18n("This menuitem already exists."));
		return;
	}

	if ( filename.isEmpty() )
	{
		KMessageBox::error(this,i18n("No help file was chosen."));
		return;
	}

	TQFileInfo fi(filename);
	if ( filename.find("http://",0)!=0 && !fi.exists() )
	{
		KMessageBox::error(this,TQString(i18n("File '%1' doesn't exist.")).tqarg(filename));
		return;
	}

	accept();
}

//END UserHelpAddDialog

}

#include "userhelpdialog.moc"