summaryrefslogtreecommitdiffstats
path: root/src/kile/usermenudialog.cpp
blob: f4e1a1dc6cf05599df324dc6133b9dd2d713c9c4 (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
/***************************************************************************
    begin                : Sun Jun 3 2001
    copyright            : (C) 2001 - 2003 by Brachet Pascal, 2003 Jeroen Wijnhout
    email                : Jeroen.Wijnhout@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 "usermenudialog.h"

#include <tqlabel.h>
#include <tqlineedit.h>
#include <tqlayout.h>
#include <tqcombobox.h>
#include <tqregexp.h>
#include <tqradiobutton.h>
#include <tqbuttongroup.h>

#include <kpushbutton.h>
#include <klocale.h>
#include <ktextedit.h>
#include "kiledebug.h"

namespace KileDialog
{

UserTags::UserTags(const TQValueList<KileAction::TagData> &list, TQWidget* tqparent,  const char* name, const TQString &caption)
    : 	KDialogBase(tqparent,name,true,caption,KDialogBase::Apply|KDialogBase::Cancel, KDialogBase::Apply, true),
	m_list(list)
{
 	TQWidget *page = new TQWidget( this );
	setMainWidget(page);
	TQGridLayout *gbox = new TQGridLayout( page, 6, 3,5,5,"");
  	gbox->addRowSpacing( 0, fontMetrics().lineSpacing() );

	m_combo=new TQComboBox(page,"combo");
	connect(m_combo, TQT_SIGNAL(activated(int)),this,TQT_SLOT(change(int)));

	m_labelName = new TQLabel( page, "label1" );
	m_labelName->setText(i18n("Menu item:"));
	m_editName =new TQLineEdit(page,"name");

	m_labelTag = new TQLabel( page, "label2" );
	m_labelTag->setText(i18n("Value:"));
	m_editTag=new KTextEdit(page,"tag");
	m_editTag->setTextFormat(TQt::PlainText);

	m_buttonAdd = new KPushButton(i18n("Add"),page);
	m_buttonInsert = new KPushButton(i18n("Insert"),page);
	m_buttonRemove = new KPushButton(i18n("Remove"),page);

	connect(m_buttonAdd, TQT_SIGNAL(clicked()) , this , TQT_SLOT(slotAdd()));
	connect(m_buttonInsert, TQT_SIGNAL(clicked()) , this , TQT_SLOT(slotInsert()));
	connect(m_buttonRemove, TQT_SIGNAL(clicked()) , this , TQT_SLOT(slotRemove()));

	gbox->addMultiCellWidget(m_combo,0,0,0,2,0);
	gbox->addMultiCellWidget(m_labelName,1,1,0,2,0);
	gbox->addMultiCellWidget(m_editName,2,2,0,2,0);
	gbox->addMultiCellWidget(m_labelTag,3,3,0,2,0);
	gbox->addMultiCellWidget(m_editTag,4,4,0,2,0);
	gbox->addWidget(m_buttonAdd, 5,0, TQt::AlignLeft);
	gbox->addWidget(m_buttonInsert, 5,1, TQt::AlignLeft);
	gbox->addWidget(m_buttonRemove, 5,2, TQt::AlignLeft);

	resize(350,150);

	m_prevIndex=0;
	redraw();
}

UserTags::~UserTags()
{
}

void UserTags::redraw()
{
	KILE_DEBUG() << TQString("usermenudialog redraw() m_prevIndex = %1, m_list.size() = %2").tqarg(m_prevIndex).tqarg(m_list.size()) << endl;
	m_combo->clear();

	if (m_list.size() > 0)
	{
		for (uint i=0; i<m_list.size(); ++i)
		{
			m_combo->insertItem( TQString::number(i+1) + ": " + m_list[i].text );
		}
		m_combo->setCurrentItem(m_prevIndex);

		m_editTag->setText( completeTag(m_list[m_prevIndex]) );
		m_editName->setText(m_list[m_prevIndex].text);
	}
	else
	{
		m_editTag->setText("");
		m_editName->setText("");
	}
}

void UserTags::change(int index)
{
	KILE_DEBUG() << TQString("usermenudialog: change(%1) prev %2").tqarg(index).tqarg(m_prevIndex) << endl;
	m_list[m_prevIndex] = splitTag(m_editName->text(), m_editTag->text());

	m_combo->changeItem(TQString::number(m_prevIndex+1)+": "+m_list[m_prevIndex].text, m_prevIndex);

	m_editTag->setText( completeTag(m_list[index]) );
	m_editName->setText(m_list[index].text);

	m_prevIndex=index;
}

void UserTags::slotApply()
{
	//store current values before exiting
	if (m_list.count() > 0 )
		m_list[m_prevIndex] = splitTag(m_editName->text(), m_editTag->text());

	KILE_DEBUG() << "usermenudialog: slotApply" << endl;
	accept();
}

void UserTags::slotAdd()
{
	m_list.append(splitTag(m_editName->text(), m_editTag->text()));

	m_prevIndex = m_list.count() - 1;
	redraw();
}

void UserTags::slotInsert()
{
	m_list.insert(m_list.at(m_prevIndex), splitTag(m_editName->text(), m_editTag->text()));
	redraw();
}

void UserTags::slotRemove()
{
	if (m_list.size() > 0)
	{
		m_list.remove( m_list.at(m_prevIndex) );

		--m_prevIndex;
		if (m_prevIndex >= static_cast<int>(m_list.count()) ) m_prevIndex = m_list.count()-1;
		if (m_prevIndex < 0 ) m_prevIndex=0;

		redraw();
	}
}

TQString UserTags::completeTag(const KileAction::TagData & td)
{
	if ( td.tagEnd.length() == 0 )
		return td.tagBegin;
	else
		return td.tagBegin + "%M" + td.tagEnd;
}

KileAction::TagData UserTags::splitTag(const TQString & name, const TQString & tag)
{
	TQStringList parts = TQStringList::split("%M", tag);
	int dx = parts[0].length();
	if ( parts[1].length() == 0 )
	{
		int i = parts[0].find(TQRegExp("[\\[\\{\\(]"));
		if ( i != -1 )
			dx = i + 1;
	}

	return KileAction::TagData(name, parts[0], parts[1], dx, 0, TQString()); 
}

}

#include "usermenudialog.moc"