summaryrefslogtreecommitdiffstats
path: root/src/kile/mathenvdialog.cpp
blob: a6df529d5bc79ede706c00d1f5eb7823db21d755 (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
/***************************************************************************
                           mathenvdialog.cpp
----------------------------------------------------------------------------
    date                 : Dec 06 2005
    version              : 0.21
    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 "mathenvdialog.h"
#include "codecompletion.h"
#include "kileedit.h"

#include <tqlayout.h>
#include <tqstringlist.h>
#include <tqvaluelist.h>
#include <tqwhatsthis.h>

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

namespace KileDialog 
{

MathEnvironmentDialog::MathEnvironmentDialog(TQWidget *tqparent, KConfig *config, KileInfo *ki, KileDocument::LatexCommands *commands) 
	: Wizard(config,tqparent), m_ki(ki), m_latexCommands(commands)
{
	TQWidget *page = new TQWidget(this);
	setMainWidget(page);
	setCaption(i18n("Math Environments"));
	
	TQVBoxLayout *vbox = new TQVBoxLayout(page,8,8);
	
	// environment groupbox
	TQButtonGroup *envgroup = new TQButtonGroup( i18n("Environment"),page);
	envgroup->setColumnLayout(0, Qt::Vertical );
	envgroup->tqlayout()->setSpacing( 6 );
	envgroup->tqlayout()->setMargin( 11 );

	m_lbEnvironment = new TQLabel(i18n("&Name:"),envgroup);
	m_lbStarred = new TQLabel(i18n("Without n&umbering:"),envgroup);
	m_lbRows = new TQLabel(i18n("Number of &rows:"),envgroup);
	m_lbCols = new TQLabel(i18n("Number of c&ols:"),envgroup);
	m_lbSpace = new TQLabel(i18n("Space command\nto &separate groups:"), envgroup);
	m_lbTabulator = new TQLabel(i18n("Standard &tabulator:"), envgroup);
	m_lbDisplaymath = new TQLabel(i18n("Display&math mode:"), envgroup);
	m_lbBullets = new TQLabel(i18n("Use &bullets:"), envgroup);
	
	TQFrame *frame = new TQFrame(envgroup);
	frame->setFrameStyle(TQFrame::HLine | TQFrame::Sunken);
	frame->setLineWidth(1);
	
	m_coEnvironment = new TQComboBox(envgroup);	
	m_cbStarred = new TQCheckBox(envgroup);
	m_spRows = new TQSpinBox(1,99,1,envgroup);
	m_spRows->setValue(3);
	m_spCols = new TQSpinBox(1,49,1,envgroup);
	m_spCols->setValue(3);
	m_edSpace = new KLineEdit("",envgroup);
	m_coTabulator = new TQComboBox(envgroup);
	m_coDisplaymath = new TQComboBox(envgroup);	
	m_cbBullets = new TQCheckBox(envgroup);
	
	TQGridLayout *envtqlayout = new TQGridLayout( envgroup->tqlayout() );
	envtqlayout->tqsetAlignment( TQt::AlignTop );
	envtqlayout->addWidget( m_lbEnvironment, 0,0 ); 
	envtqlayout->addWidget( m_lbStarred, 1,0 );
	envtqlayout->addWidget( m_lbRows, 2,0 );
	envtqlayout->addWidget( m_lbCols, 3,0 );
	envtqlayout->addWidget( m_lbTabulator, 5,0 ); 
	envtqlayout->addWidget( m_lbDisplaymath, 6,0 ); 
	envtqlayout->addWidget( m_coEnvironment, 0,1 );
	envtqlayout->addWidget( m_cbStarred, 1,1 );
	envtqlayout->addWidget( m_spRows, 2,1 );
	envtqlayout->addWidget( m_spCols, 3,1 );
	envtqlayout->addWidget( m_coTabulator, 5,1 );
	envtqlayout->addWidget( m_coDisplaymath, 6,1 );
	envtqlayout->addWidget( m_lbSpace, 3,3 );
	envtqlayout->addWidget( m_lbBullets, 5,3 );
	envtqlayout->addWidget( m_edSpace, 3,4 );
	envtqlayout->addWidget( m_cbBullets, 5,4 );	
	envtqlayout->addMultiCellWidget(frame,4,4,0,4);
	envtqlayout->setRowSpacing(4,30);
	envtqlayout->setColSpacing(2,20);
	envtqlayout->setColStretch(4,1);
	
	// add widgets
	vbox->addWidget( envgroup);
	vbox->addStretch(1);

	m_lbEnvironment->setBuddy(m_coEnvironment);
	m_lbStarred->setBuddy(m_cbStarred);
	m_lbRows->setBuddy(m_spRows);
	m_lbCols->setBuddy(m_spCols);
	m_lbSpace->setBuddy(m_edSpace);
	m_lbTabulator->setBuddy(m_coTabulator);
	m_lbDisplaymath->setBuddy(m_coDisplaymath);
	m_lbBullets->setBuddy(m_cbBullets);

	// initialize dialog
	m_coDisplaymath->insertItem(TQString());
	m_coDisplaymath->insertItem("displaymath");
	m_coDisplaymath->insertItem("\\[");
	m_coDisplaymath->insertItem("equation");
	m_coDisplaymath->insertItem("equation*");
	
	// install environments
	initEnvironments();
	m_coEnvironment->setCurrentText("align");
	slotEnvironmentChanged( m_coEnvironment->currentItem() );
	
	// signals and slots
	connect(m_coEnvironment, TQT_SIGNAL(activated(int)), this, TQT_SLOT(slotEnvironmentChanged(int)));
	connect(m_spCols, TQT_SIGNAL(valueChanged(int)), this, TQT_SLOT(slotSpinboxValueChanged(int)));
	
	TQWhatsThis::add(m_coEnvironment,i18n("Choose an environment."));
	TQWhatsThis::add(m_cbStarred,i18n("Use the starred version of this environment."));
	TQWhatsThis::add(m_spRows,i18n("Choose the number of table rows."));
	TQWhatsThis::add(m_spCols,i18n("Choose the number of table columns or tqalignment groups."));
	TQWhatsThis::add(m_edSpace,i18n("Define an extra LaTeX command to separate tqalignment groups."));
	TQWhatsThis::add(m_coTabulator,i18n("Choose one of some predefined tabulators."));
	TQWhatsThis::add(m_coDisplaymath,i18n("Some environments are only valid in math mode. You can surround them with one of these display math modes."));
	TQWhatsThis::add(m_cbBullets,i18n("Insert bullets in each cell. Alt+Ctrl+Right and Alt+Ctrl+Left will move very quick from one cell to another."));
}

void MathEnvironmentDialog::initEnvironments()
{
	// read all math environments and insert them into the combobox
	TQStringList list;
	TQStringList::ConstIterator it;
	m_latexCommands->commandList(list,(uint)(KileDocument::CmdAttrAmsmath|KileDocument::CmdAttrMath),false);
	for ( it=list.begin(); it != list.end(); ++it ) 
	{
		m_coEnvironment->insertItem(*it);
	}
}

bool MathEnvironmentDialog::isGroupsParameterEnv()
{
	return ( m_parameter == "{n}" );
}

bool MathEnvironmentDialog::isParameterEnv()
{
	return ( m_parameter.tqfind("{") >= 0 );
}

////////////////////////////// determine the whole tag //////////////////////////////

void MathEnvironmentDialog::slotEnvironmentChanged(int index)
{
	KILE_DEBUG() << "environment changed: " << m_coEnvironment->text(index) << endl;
	m_envname = m_coEnvironment->text(index);
	
	// look for environment parameter in dictionary
	KileDocument::LatexCmdAttributes attr;
	if ( m_latexCommands->commandAttributes(m_envname,attr) ) 
	{
		m_starred = attr.starred;
		m_mathmode = attr.mathmode;
		m_columns = ( attr.tabulator == "&" );
		m_groups  = ( attr.tabulator == "&=" );
		m_fixedcolumns  = ( attr.tabulator == "&=&" );
		m_tabulator = attr.tabulator;
		m_parameter = attr.parameter;
	}	
	
	// set starred version
	m_cbStarred->setChecked(false);
	m_lbStarred->setEnabled(m_starred);
	m_cbStarred->setEnabled(m_starred);
	
	// determine column/group entries
	TQString labeltext = i18n("Number of cols:");
	bool spinstate = false;
	int minvalue = 1;
	int maxvalue = 1;
	int value = 1;
	
	if ( m_columns ) 
	{
		spinstate = true;
		if ( m_envname != "cases" )          // 1,49,3 
		{       
			maxvalue = 49;
			value = 3;
		} 
		else 
		{
			minvalue = 2;                     // 2,2,2 
			maxvalue = 2;
			value = 2;
		}
	} else if ( m_groups ) 
	{
		spinstate = true;
		labeltext = i18n("Number of groups:");
		maxvalue = 19;                       // 1,19,1 
	} else if ( m_fixedcolumns ) 
	{
		spinstate = true;
		minvalue = 3;                        // 3,3,3 
		maxvalue = 3;
		value = 3;
	} else if ( m_envname == "split" ) 
	{   
		spinstate = true;
		maxvalue = 2;                        // 1,2,1
	} 
 
	// set column/group entries
	m_lbCols->setText(labeltext);
	m_spCols->setMinValue(minvalue);
	m_spCols->setMaxValue(maxvalue);
	m_spCols->setValue(value);	
	
	m_lbCols->setEnabled(spinstate);
	m_spCols->setEnabled(spinstate);
	slotSpinboxValueChanged(m_spCols->value());
	
	// set tabulator entries
	m_coTabulator->clear(); 
	TQStringList tablist;
	if ( m_tabulator == "&=&" ) 
		tablist << "&=&" << "& &" << "&<&" << "&<=&" << "&>&" << "&>=&"
	           << "&\\ne&" << "&\\approx&" << "&\\equiv&" << "&\\conq&" ;
	else if ( m_tabulator == "&=" ) 
		tablist << "&=" << "& " << "&<" << "&<=" << "&>" << "&>="
	           << "&\\ne" << "&\\approx" << "&\\equiv" << "&\\conq" ;
	else if ( ! m_tabulator.isEmpty() )
		tablist << "&";
	bool tabstate = ( tablist.count() > 0 );
	m_lbTabulator->setEnabled(tabstate);
	m_coTabulator->setEnabled(tabstate);
	if ( tabstate ) 
		m_coTabulator->insertStringList(tablist); 
	
	// set displaymathmode entries
	m_lbDisplaymath->setEnabled(m_mathmode);
	m_coDisplaymath->setEnabled(m_mathmode);
}

void MathEnvironmentDialog::slotSpinboxValueChanged(int index)
{	
	bool state = ( index>1 && m_groups && isGroupsParameterEnv() );
	m_lbSpace->setEnabled(state);
	m_edSpace->setEnabled(state);
}

void MathEnvironmentDialog::slotOk()
{
	// environment
	TQString envname = ( m_cbStarred->isChecked() ) ? m_envname + '*' : m_envname;
	TQString indent = m_ki->editorExtension()->autoIndentEnvironment();
	
	// use bullets?
	TQString bullet = ( m_cbBullets->isChecked() ) ? s_bullet : TQString();
	
	// normal tabulator
	TQString tab = m_coTabulator->currentText();
	tab.tqreplace("<=","\\le");
	tab.tqreplace(">=","\\ge");
	TQString tabulator = bullet + ' ' + tab + ' ';
	
	// number of rows
	int numrows = m_spRows->value();
	
	// get number of groups/columns and tabulator to separate these
	TQString topgrouptabulator,grouptabulator;
	int numgroups;
	bool aligngroups;
	if ( m_groups ) 
	{
		aligngroups = true;
		numgroups = ( m_tabulator != "&" ) ? m_spCols->value() : 1;
		if ( m_edSpace->isEnabled() ) 
		{
			TQString spaces;
			topgrouptabulator = "  &" + m_edSpace->text() + "  ";
			grouptabulator = "  &  " + spaces.fill(' ', m_edSpace->text().length());
		} 
		else 
		{
			topgrouptabulator = "  &  ";
			grouptabulator = "  &  ";
		}
	} 
	else 
	{
		aligngroups = false;
		if ( ! m_fixedcolumns )
			numgroups = ( m_columns ) ? m_spCols->value()-1 : 0;
		else 
			numgroups = 1;
	}
		
	// get displaymath mode
	TQString displaymathbegin = TQString();
	TQString displaymathend = TQString();
	if ( m_coDisplaymath->isEnabled() ) 
	{
		TQString mathmode = m_coDisplaymath->currentText();
		if ( ! mathmode.isEmpty() ) 
		{
			if ( mathmode == "\\[" ) 
			{
				displaymathbegin = "\\[\n";
				displaymathend   = "\\]\n";
			} 
			else 
			{
				displaymathbegin = TQString("\\begin{%1}\n").tqarg(mathmode);
				displaymathend   = TQString("\\end{%1}\n").tqarg(mathmode);
			}
		}
	}
		 
	// build tag 
	m_td.tagBegin = displaymathbegin;
	
	TQString parameter;
	if ( isGroupsParameterEnv() )
		parameter = TQString("{%2}").tqarg(numgroups);
	else if ( isParameterEnv() )
		parameter = '{' + bullet + '}';

	// open environment  
	m_td.tagBegin += TQString("\\begin{%1}").tqarg(envname) + parameter + '\n';
	
	for ( int row=0; row<numrows; ++row ) 
	{
		m_td.tagBegin += indent;
		for ( int col=0; col<numgroups; ++col ) 
		{
			m_td.tagBegin += tabulator; 
			// is there more than one group or column?
			if ( aligngroups && col<numgroups-1 ) 
			{
				if ( row == 0 ) 
					m_td.tagBegin += bullet + topgrouptabulator; 
				else
					m_td.tagBegin += bullet + grouptabulator; 
			}
		}
		// last row without CR
		if ( row < numrows-1 )
			m_td.tagBegin += bullet + " \\\\\n"; 
		else
			m_td.tagBegin += bullet; 
	}
	
	// close environment
	m_td.tagEnd = TQString("\n\\end{%1}\n").tqarg(envname);
	m_td.tagEnd += displaymathend;
	
	m_td.dy = ( displaymathbegin.isEmpty() ) ? 1 : 2;
	m_td.dx = indent.length();

	accept();
}

}

#include "mathenvdialog.moc"