smalldialogs.cpp 22.2 KB
Newer Older
Joachim Eibl's avatar
Joachim Eibl committed
1
/***************************************************************************
Joachim Eibl's avatar
Joachim Eibl committed
2
 *   Copyright (C) 2005-2007 by Joachim Eibl                               *
Joachim Eibl's avatar
Joachim Eibl committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 *   joachim.eibl at gmx.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.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA.           *
 ***************************************************************************/

#include "smalldialogs.h"
22
#include "options.h"
Joachim Eibl's avatar
Joachim Eibl committed
23
24
25
26
27
28
29
30
31
32
#include "diff.h"

#include <QComboBox>
#include <QCheckBox>
#include <QLineEdit>
#include <QLabel>
#include <QLayout>
#include <QToolTip>
#include <QUrl>
#include <QDir>
Joachim Eibl's avatar
Joachim Eibl committed
33
#include <QDropEvent>
Joachim Eibl's avatar
Joachim Eibl committed
34
#include <QMenu>
Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
35
#include <QPushButton>
Michael Reeves's avatar
Michael Reeves committed
36
#include <QMimeData>
Joachim Eibl's avatar
Joachim Eibl committed
37
38
39

#include <kfiledialog.h>
#include <klocale.h>
Michael Reeves's avatar
Michael Reeves committed
40
#include <kdebug.h>
Joachim Eibl's avatar
Joachim Eibl committed
41
42
43
44
45

// OpenDialog **************************************************************

OpenDialog::OpenDialog(
   QWidget* pParent, const QString& n1, const QString& n2, const QString& n3,
46
   bool bMerge, const QString& outputName, const char* slotConfigure, Options* pOptions )
Joachim Eibl's avatar
Joachim Eibl committed
47
: QDialog( pParent )
Joachim Eibl's avatar
Joachim Eibl committed
48
{
Joachim Eibl's avatar
Joachim Eibl committed
49
50
   setObjectName("OpenDialog");
   setModal(true);
Joachim Eibl's avatar
Joachim Eibl committed
51
52
   m_pOptions = pOptions;

Joachim Eibl's avatar
Joachim Eibl committed
53
54
55
56
57
58
   QVBoxLayout* v = new QVBoxLayout( this );
   v->setMargin(5);
   QGridLayout* h = new QGridLayout();
   v->addLayout(h);
   h->setSpacing( 5 );
   h->setColumnStretch( 1, 10 );
Joachim Eibl's avatar
Joachim Eibl committed
59
60
61

   QLabel* label  = new QLabel( i18n("A (Base):"), this );

Joachim Eibl's avatar
Joachim Eibl committed
62
63
64
   m_pLineA = new QComboBox();
   m_pLineA->setEditable(true);
   m_pLineA->insertItems( 0, m_pOptions->m_recentAFiles );
Michael Reeves's avatar
Michael Reeves committed
65
   m_pLineA->setEditText( QUrl(n1).toDisplayString() );
Joachim Eibl's avatar
Joachim Eibl committed
66
   m_pLineA->setMinimumWidth( 200 );
Joachim Eibl's avatar
Joachim Eibl committed
67
   QPushButton * button = new QPushButton( i18n("File..."), this );
68
   connect(button, &QPushButton::clicked, this, &OpenDialog::selectFileA);
Joachim Eibl's avatar
Joachim Eibl committed
69
   QPushButton * button2 = new QPushButton( i18n("Dir..."), this );
70
71
   connect(button2, &QPushButton::clicked, this, &OpenDialog::selectDirA);
   connect(m_pLineA, &QComboBox::editTextChanged, this, &OpenDialog::inputFilenameChanged);
Joachim Eibl's avatar
Joachim Eibl committed
72
73
74
75
76
77
78

   h->addWidget( label,    0, 0 );
   h->addWidget( m_pLineA,  0, 1 );
   h->addWidget( button,   0, 2 );
   h->addWidget( button2,  0, 3 );

   label    = new QLabel( "B:", this );
Joachim Eibl's avatar
Joachim Eibl committed
79
80
81
   m_pLineB  = new QComboBox();
   m_pLineB->setEditable(true);
   m_pLineB->insertItems( 0, m_pOptions->m_recentBFiles );
Michael Reeves's avatar
Michael Reeves committed
82
   m_pLineB->setEditText( QUrl(n2).toDisplayString() );
Joachim Eibl's avatar
Joachim Eibl committed
83
   m_pLineB->setMinimumWidth( 200 );
Joachim Eibl's avatar
Joachim Eibl committed
84
   button   = new QPushButton( i18n("File..."), this );
85
   connect(button, &QPushButton::clicked, this, &OpenDialog::selectFileB);
Joachim Eibl's avatar
Joachim Eibl committed
86
   button2   = new QPushButton( i18n("Dir..."), this );
87
88
   connect(button2, &QPushButton::clicked, this, &OpenDialog::selectDirB);
   connect(m_pLineB, &QComboBox::editTextChanged, this, &OpenDialog::inputFilenameChanged);
Joachim Eibl's avatar
Joachim Eibl committed
89
90

   h->addWidget( label,     1, 0 );
Joachim Eibl's avatar
Joachim Eibl committed
91
   h->addWidget( m_pLineB,  1, 1 );
Joachim Eibl's avatar
Joachim Eibl committed
92
93
94
95
   h->addWidget( button,    1, 2 );
   h->addWidget( button2,   1, 3 );

   label  = new QLabel( i18n("C (Optional):"), this );
Joachim Eibl's avatar
Joachim Eibl committed
96
97
98
   m_pLineC= new QComboBox();
   m_pLineC->setEditable(true);
   m_pLineC->insertItems( 0, m_pOptions->m_recentCFiles );
Michael Reeves's avatar
Michael Reeves committed
99
   m_pLineC->setEditText( QUrl(n3).toDisplayString() );
Joachim Eibl's avatar
Joachim Eibl committed
100
   m_pLineC->setMinimumWidth( 200 );
Joachim Eibl's avatar
Joachim Eibl committed
101
   button = new QPushButton( i18n("File..."), this );
102
   connect(button, &QPushButton::clicked, this, &OpenDialog::selectFileC);
Joachim Eibl's avatar
Joachim Eibl committed
103
   button2   = new QPushButton( i18n("Dir..."), this );
104
105
   connect(button2, &QPushButton::clicked, this, &OpenDialog::selectDirC);
   connect(m_pLineC, &QComboBox::editTextChanged, this, &OpenDialog::inputFilenameChanged);
Joachim Eibl's avatar
Joachim Eibl committed
106
107
108
109
110
111
112
113
114

   h->addWidget( label,     2, 0 );
   h->addWidget( m_pLineC,   2, 1 );
   h->addWidget( button,    2, 2 );
   h->addWidget( button2,   2, 3 );

   m_pMerge = new QCheckBox( i18n("Merge"), this );
   h->addWidget( m_pMerge, 3, 0 );

Joachim Eibl's avatar
Joachim Eibl committed
115
   QHBoxLayout* hl = new QHBoxLayout();
Joachim Eibl's avatar
Joachim Eibl committed
116
117
118
119
120
121
   h->addLayout( hl, 3, 1 );
   hl->addStretch(2);
   button = new QPushButton(i18n("Swap/Copy Names ..."), this);
   //button->setToggleButton(false);
   hl->addWidget( button );

Joachim Eibl's avatar
Joachim Eibl committed
122
   QMenu* m = new QMenu(this);
Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
123
124
125
126
127
128
129
130
131
   m->addAction( i18n("Swap %1<->%2", QString("A"),QString("B") ));
   m->addAction( i18n("Swap %1<->%2",QString("B"),QString("C") ));
   m->addAction( i18n("Swap %1<->%2",QString("C"),QString("A") ));
   m->addAction( i18n("Copy %1->Output",QString("A")  ));
   m->addAction( i18n("Copy %1->Output",QString("B")  ));
   m->addAction( i18n("Copy %1->Output",QString("C")  ));
   m->addAction( i18n("Swap %1<->Output",QString("A") ));
   m->addAction( i18n("Swap %1<->Output",QString("B") ));
   m->addAction( i18n("Swap %1<->Output",QString("C") ));
132
   connect(m, &QMenu::triggered, this, &OpenDialog::slotSwapCopyNames);
Joachim Eibl's avatar
Joachim Eibl committed
133
   button->setMenu(m);
Joachim Eibl's avatar
Joachim Eibl committed
134
135
136
137
138


   hl->addStretch(2);

   label  = new QLabel( i18n("Output (optional):"), this );
Joachim Eibl's avatar
Joachim Eibl committed
139
140
141
   m_pLineOut = new QComboBox();
   m_pLineOut->setEditable(true);
   m_pLineOut->insertItems( 0, m_pOptions->m_recentOutputFiles );
Michael Reeves's avatar
Michael Reeves committed
142
   m_pLineOut->setEditText( QUrl(outputName).toDisplayString() );
Joachim Eibl's avatar
Joachim Eibl committed
143
   m_pLineOut->setMinimumWidth( 200 );
Joachim Eibl's avatar
Joachim Eibl committed
144
   button = new QPushButton( i18n("File..."), this );
145
   connect(button, &QPushButton::clicked, this, &OpenDialog::selectOutputName);
Joachim Eibl's avatar
Joachim Eibl committed
146
   button2   = new QPushButton( i18n("Dir..."), this );
147
148
149
150
151
   connect(button2, &QPushButton::clicked, this, &OpenDialog::selectOutputDir);
   connect(m_pMerge, &QCheckBox::stateChanged, this, &OpenDialog::internalSlot);
   connect(this, &OpenDialog::internalSignal, m_pLineOut, &QComboBox::setEnabled);
   connect(this, &OpenDialog::internalSignal, button, &QPushButton::setEnabled);
   connect(this, &OpenDialog::internalSignal, button2, &QPushButton::setEnabled);
Joachim Eibl's avatar
Joachim Eibl committed
152
153
154
155
156
157
158
159
160
161
162
163

   m_pMerge->setChecked( !bMerge );
   m_pMerge->setChecked( bMerge );
//   m_pLineOutput->setEnabled( bMerge );

//   button->setEnabled( bMerge );

   h->addWidget( label,          4, 0 );
   h->addWidget( m_pLineOut,      4, 1 );
   h->addWidget( button,         4, 2 );
   h->addWidget( button2,        4, 3 );

Joachim Eibl's avatar
Joachim Eibl committed
164
   h->addItem(new QSpacerItem(200, 0), 0, 1);
Joachim Eibl's avatar
Joachim Eibl committed
165

Joachim Eibl's avatar
Joachim Eibl committed
166
167
168
   QHBoxLayout* l = new QHBoxLayout();
   v->addLayout( l );
   l->setSpacing(5);
Joachim Eibl's avatar
Joachim Eibl committed
169
170
171
172
173
174
175
176
177

   button = new QPushButton( i18n("Configure..."), this );
   connect( button, SIGNAL(clicked()), pParent, slotConfigure );
   l->addWidget( button, 1 );

   l->addStretch(1);

   button = new QPushButton( i18n("&OK"), this );
   button->setDefault( true );
178
   connect(button, &QPushButton::clicked, this, &OpenDialog::accept);
Joachim Eibl's avatar
Joachim Eibl committed
179
180
181
   l->addWidget( button, 1 );

   button = new QPushButton( i18n("&Cancel"), this );
182
   connect(button, &QPushButton::clicked, this, &OpenDialog::reject);
Joachim Eibl's avatar
Joachim Eibl committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
   l->addWidget( button,1 );

   QSize sh = sizeHint();
   setFixedHeight( sh.height() );
   m_bInputFileNameChanged = false;

#ifdef KREPLACEMENTS_H
   m_pLineA->lineEdit()->installEventFilter( this );
   m_pLineB->lineEdit()->installEventFilter( this );
   m_pLineC->lineEdit()->installEventFilter( this );
   m_pLineOut->lineEdit()->installEventFilter( this );
#endif
}

// Eventfilter: Only needed under Windows.
// Without this, files dropped in the line edit have URL-encoding.
// This eventfilter decodes the filenames as needed by KDiff3.
bool OpenDialog::eventFilter(QObject* o, QEvent* e)
{
Joachim Eibl's avatar
Joachim Eibl committed
202
203
204
205
206
207
   if ( e->type()==QEvent::DragEnter )
   {
      QDragEnterEvent* d = static_cast<QDragEnterEvent*>(e);
      d->setAccepted( d->mimeData()->hasUrls() );
      return true;
   }
Joachim Eibl's avatar
Joachim Eibl committed
208
209
210
211
   if (e->type()==QEvent::Drop)
   {
      QDropEvent* d = static_cast<QDropEvent*>(e);

Joachim Eibl's avatar
Joachim Eibl committed
212
      if ( !d->mimeData()->hasUrls() )
Joachim Eibl's avatar
Joachim Eibl committed
213
214
         return false;

Joachim Eibl's avatar
Joachim Eibl committed
215
      QList<QUrl> lst = d->mimeData()->urls();
Joachim Eibl's avatar
Joachim Eibl committed
216
217
218

      if ( lst.count() > 0 )
      {
Joachim Eibl's avatar
Joachim Eibl committed
219
         static_cast<QLineEdit*>(o)->setText( QDir::toNativeSeparators( lst[0].toLocalFile() ) );
Joachim Eibl's avatar
Joachim Eibl committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
         static_cast<QLineEdit*>(o)->setFocus();
      }
       
      return true;
   }
   return false;
}


void OpenDialog::selectURL( QComboBox* pLine, bool bDir, int i, bool bSave )
{
   QString current = pLine->currentText();
   if (current.isEmpty() && i>3 ){  current = m_pLineC->currentText(); }
   if (current.isEmpty()        ){  current = m_pLineB->currentText(); }
   if (current.isEmpty()        ){  current = m_pLineA->currentText(); }
Michael Reeves's avatar
Michael Reeves committed
235
   
236
  /*QUrl newURL = bDir ? KFileDialog::getExistingDirectoryUrl( current, this)
Michael Reeves's avatar
Michael Reeves committed
237
238
239
		    : bSave ? KFileDialog::getSaveUrl( current, "all/allfiles", this)
			    : KFileDialog::getOpenUrl( current, "all/allfiles", this);*/

240
   QUrl 			newURL;
Michael Reeves's avatar
Michael Reeves committed
241
242
243
244
245
246
247
248
249
250
251
252
253
254
   if(!bDir){
     newURL = bSave ? KFileDialog::getSaveUrl( current, "all/allfiles", this)
		      : KFileDialog::getOpenUrl( current, "all/allfiles", this);
   }
   else{
      KFileDialog 			dlg(current, "all/allfiles", this);

      //Sadlly KFileDialog::getExistingDirectoryUrl doesn't do this. Instead it presents a very akword windows 95 like interface.
      dlg.setOperationMode( KFileDialog::Opening );
      dlg.setMode( KFile::Directory | KFile::ExistingOnly );
      dlg.setWindowTitle(i18n("Select Directory"));
      dlg.exec();
      newURL = dlg.selectedUrl();
   }
Joachim Eibl's avatar
Joachim Eibl committed
255
256
   if ( !newURL.isEmpty() )
   {
Michael Reeves's avatar
Michael Reeves committed
257
258
259
260
        /*
        Since we are selecting a directory open in the parent directory
	not the one selected.
      */
Michael Reeves's avatar
Michael Reeves committed
261
       KFileDialog::setStartDir ( KIO::upUrl(newURL) );
Michael Reeves's avatar
Michael Reeves committed
262
       pLine->setEditText( newURL.url() );
Joachim Eibl's avatar
Joachim Eibl committed
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
   }
   // newURL won't be modified if nothing was selected.
}

void OpenDialog::selectFileA()     {  selectURL( m_pLineA,    false, 1, false );  }
void OpenDialog::selectFileB()     {  selectURL( m_pLineB,    false, 2, false );  }
void OpenDialog::selectFileC()     {  selectURL( m_pLineC,    false, 3, false );  }
void OpenDialog::selectOutputName(){  selectURL( m_pLineOut,  false, 4, true );  }
void OpenDialog::selectDirA()      {  selectURL( m_pLineA,    true,  1, false );  }
void OpenDialog::selectDirB()      {  selectURL( m_pLineB,    true,  2, false );  }
void OpenDialog::selectDirC()      {  selectURL( m_pLineC,    true,  3, false );  }
void OpenDialog::selectOutputDir() {  selectURL( m_pLineOut,  true,  4, true );  }

void OpenDialog::internalSlot(int i)
{
   emit internalSignal(i!=0);
}

// Clear the output-filename when any input-filename changed, 
// because users forgot to change the output and accidently overwrote it with
// wrong data during a merge.
void OpenDialog::inputFilenameChanged()
{
   if(!m_bInputFileNameChanged)
   {
      m_bInputFileNameChanged=true;
Joachim Eibl's avatar
Joachim Eibl committed
289
      m_pLineOut->clearEditText();
Joachim Eibl's avatar
Joachim Eibl committed
290
291
292
   }
}

Joachim Eibl's avatar
Joachim Eibl committed
293
294
295
296
297
298
299
300
301
302
303
304
305
306
static void fixCurrentText( QComboBox* pCB )
{
   QString s = pCB->currentText();

   int pos = s.indexOf( '\n' );
   if ( pos>=0 )
      s=s.left(pos);
   pos = s.indexOf( '\r' );
   if ( pos>=0 )
      s=s.left(pos);

   pCB->setEditText( s );
}

Joachim Eibl's avatar
Joachim Eibl committed
307
308
void OpenDialog::accept()
{
Joachim Eibl's avatar
Joachim Eibl committed
309
   int maxNofRecentFiles = 10;
Joachim Eibl's avatar
Joachim Eibl committed
310
   fixCurrentText( m_pLineA );
Joachim Eibl's avatar
Joachim Eibl committed
311
312

   QString s = m_pLineA->currentText();
Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
313
   s = QUrl::fromLocalFile(s).toLocalFile();
Joachim Eibl's avatar
Joachim Eibl committed
314
315
   QStringList* sl = &m_pOptions->m_recentAFiles;
   // If an item exist, remove it from the list and reinsert it at the beginning.
Joachim Eibl's avatar
Joachim Eibl committed
316
   sl->removeAll(s);
Joachim Eibl's avatar
Joachim Eibl committed
317
   if ( !s.isEmpty() ) sl->prepend( s );
Joachim Eibl's avatar
Joachim Eibl committed
318
   if (sl->count()>maxNofRecentFiles) sl->erase( sl->begin()+maxNofRecentFiles, sl->end() );
Joachim Eibl's avatar
Joachim Eibl committed
319

Joachim Eibl's avatar
Joachim Eibl committed
320
   fixCurrentText( m_pLineB );
Joachim Eibl's avatar
Joachim Eibl committed
321
   s = m_pLineB->currentText();
Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
322
   s = QUrl::fromLocalFile(s).toLocalFile();
Joachim Eibl's avatar
Joachim Eibl committed
323
   sl = &m_pOptions->m_recentBFiles;
Joachim Eibl's avatar
Joachim Eibl committed
324
   sl->removeAll(s);
Joachim Eibl's avatar
Joachim Eibl committed
325
   if ( !s.isEmpty() ) sl->prepend( s );
Joachim Eibl's avatar
Joachim Eibl committed
326
   if (sl->count()>maxNofRecentFiles) sl->erase( sl->begin()+maxNofRecentFiles, sl->end() );
Joachim Eibl's avatar
Joachim Eibl committed
327

Joachim Eibl's avatar
Joachim Eibl committed
328
   fixCurrentText( m_pLineC );
Joachim Eibl's avatar
Joachim Eibl committed
329
   s = m_pLineC->currentText();
Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
330
   s = QUrl::fromLocalFile(s).toLocalFile();
Joachim Eibl's avatar
Joachim Eibl committed
331
   sl = &m_pOptions->m_recentCFiles;
Joachim Eibl's avatar
Joachim Eibl committed
332
   sl->removeAll(s);
Joachim Eibl's avatar
Joachim Eibl committed
333
   if ( !s.isEmpty() ) sl->prepend( s );
Joachim Eibl's avatar
Joachim Eibl committed
334
   if (sl->count()>maxNofRecentFiles) sl->erase( sl->begin()+maxNofRecentFiles, sl->end() );
Joachim Eibl's avatar
Joachim Eibl committed
335

Joachim Eibl's avatar
Joachim Eibl committed
336
   fixCurrentText( m_pLineOut );
Joachim Eibl's avatar
Joachim Eibl committed
337
   s = m_pLineOut->currentText();
Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
338
   s = QUrl::fromLocalFile(s).toLocalFile();
Joachim Eibl's avatar
Joachim Eibl committed
339
   sl = &m_pOptions->m_recentOutputFiles;
Joachim Eibl's avatar
Joachim Eibl committed
340
   sl->removeAll(s);
Joachim Eibl's avatar
Joachim Eibl committed
341
   if ( !s.isEmpty() ) sl->prepend( s );
Joachim Eibl's avatar
Joachim Eibl committed
342
   if (sl->count()>maxNofRecentFiles) sl->erase( sl->begin()+maxNofRecentFiles, sl->end() );
Joachim Eibl's avatar
Joachim Eibl committed
343
344
345
346

   QDialog::accept();
}

Joachim Eibl's avatar
Joachim Eibl committed
347
void OpenDialog::slotSwapCopyNames( QAction* pAction ) // id selected in the popup menu
Joachim Eibl's avatar
Joachim Eibl committed
348
{
Joachim Eibl's avatar
Joachim Eibl committed
349
   int id = pAction->parentWidget()->actions().indexOf(pAction);
Joachim Eibl's avatar
Joachim Eibl committed
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
   QComboBox* cb1=0;
   QComboBox* cb2=0;
   switch(id)
   {
   case 0:  cb1=m_pLineA; cb2=m_pLineB; break;
   case 1:  cb1=m_pLineB; cb2=m_pLineC; break;
   case 2:  cb1=m_pLineC; cb2=m_pLineA; break;
   case 3:  cb1=m_pLineA; cb2=m_pLineOut; break;
   case 4:  cb1=m_pLineB; cb2=m_pLineOut; break;
   case 5:  cb1=m_pLineC; cb2=m_pLineOut; break;
   case 6:  cb1=m_pLineA; cb2=m_pLineOut; break;
   case 7:  cb1=m_pLineB; cb2=m_pLineOut; break;
   case 8:  cb1=m_pLineC; cb2=m_pLineOut; break;
   }
   if ( cb1 && cb2 )
   {
      QString t1 = cb1->currentText();
      QString t2 = cb2->currentText();
Joachim Eibl's avatar
Joachim Eibl committed
368
      cb2->setEditText(t1);
Joachim Eibl's avatar
Joachim Eibl committed
369
370
      if ( id<=2 || id>=6 )
      {
Joachim Eibl's avatar
Joachim Eibl committed
371
         cb1->setEditText( t2 );
Joachim Eibl's avatar
Joachim Eibl committed
372
373
374
375
376
377
378
379
380
      }
   }
}

// FindDialog *********************************************

FindDialog::FindDialog(QWidget* pParent)
: QDialog( pParent )
{
Joachim Eibl's avatar
Joachim Eibl committed
381
   QGridLayout* layout = new QGridLayout( this );
Joachim Eibl's avatar
Joachim Eibl committed
382
383
384
385
   layout->setMargin(5);
   layout->setSpacing(5);

   int line=0;
Joachim Eibl's avatar
Joachim Eibl committed
386
   layout->addWidget( new QLabel(i18n("Search text:"),this), line, 0, 1, 2 );
Joachim Eibl's avatar
Joachim Eibl committed
387
388
389
   ++line;

   m_pSearchString = new QLineEdit( this );
Joachim Eibl's avatar
Joachim Eibl committed
390
   layout->addWidget( m_pSearchString, line, 0, 1, 2 );
Joachim Eibl's avatar
Joachim Eibl committed
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
   ++line;

   m_pCaseSensitive = new QCheckBox(i18n("Case sensitive"),this);
   layout->addWidget( m_pCaseSensitive, line, 1 );

   m_pSearchInA = new QCheckBox(i18n("Search A"),this);
   layout->addWidget( m_pSearchInA, line, 0 );
   m_pSearchInA->setChecked( true );
   ++line;

   m_pSearchInB = new QCheckBox(i18n("Search B"),this);
   layout->addWidget( m_pSearchInB, line, 0 );
   m_pSearchInB->setChecked( true );
   ++line;

   m_pSearchInC = new QCheckBox(i18n("Search C"),this);
   layout->addWidget( m_pSearchInC, line, 0 );
   m_pSearchInC->setChecked( true );
   ++line;

   m_pSearchInOutput = new QCheckBox(i18n("Search output"),this);
   layout->addWidget( m_pSearchInOutput, line, 0 );
   m_pSearchInOutput->setChecked( true );
   ++line;

   QPushButton* pButton = new QPushButton( i18n("&Search"), this );
   layout->addWidget( pButton, line, 0 );
418
   connect(pButton, &QPushButton::clicked, this, &FindDialog::accept);
Joachim Eibl's avatar
Joachim Eibl committed
419
420
421

   pButton = new QPushButton( i18n("&Cancel"), this );
   layout->addWidget( pButton, line, 1 );
422
   connect(pButton, &QPushButton::clicked, this, &FindDialog::reject);
Joachim Eibl's avatar
Joachim Eibl committed
423
424
425
426

   hide();
}

427
428
429
430
431
432
void FindDialog::setVisible( bool bVisible )
{
   QDialog::setVisible( bVisible );
   m_pSearchString->selectAll();
   m_pSearchString->setFocus();
}
Joachim Eibl's avatar
Joachim Eibl committed
433
434
435
436
437
438

RegExpTester::RegExpTester( QWidget* pParent, const QString& autoMergeRegExpToolTip,
   const QString& historyStartRegExpToolTip, const QString& historyEntryStartRegExpToolTip, const QString& historySortKeyOrderToolTip )
: QDialog( pParent)
{
   int line=0;
Joachim Eibl's avatar
Joachim Eibl committed
439
440
441
442
   setWindowTitle(i18n("Regular Expression Tester"));
   QGridLayout* pGrid = new QGridLayout( this );
   pGrid->setSpacing(5);
   pGrid->setMargin(5);
Joachim Eibl's avatar
Joachim Eibl committed
443
444
445

   QLabel* l = new QLabel(i18n("Auto merge regular expression:"), this);
   pGrid->addWidget(l,line,0);
Joachim Eibl's avatar
Joachim Eibl committed
446
   l->setToolTip( autoMergeRegExpToolTip );
Joachim Eibl's avatar
Joachim Eibl committed
447
448
   m_pAutoMergeRegExpEdit = new QLineEdit(this);
   pGrid->addWidget(m_pAutoMergeRegExpEdit,line,1);
449
   connect(m_pAutoMergeRegExpEdit, &QLineEdit::textChanged, this, &RegExpTester::slotRecalc);
Joachim Eibl's avatar
Joachim Eibl committed
450
451
452
   ++line;

   l = new QLabel(i18n("Example auto merge line:"), this);
453
   pGrid->addWidget(l,line,0);
Joachim Eibl's avatar
Joachim Eibl committed
454
   l->setToolTip( i18n("To test auto merge, copy a line as used in your files.") );
Joachim Eibl's avatar
Joachim Eibl committed
455
456
   m_pAutoMergeExampleEdit = new QLineEdit(this);
   pGrid->addWidget(m_pAutoMergeExampleEdit,line,1);
457
   connect(m_pAutoMergeExampleEdit, &QLineEdit::textChanged, this, &RegExpTester::slotRecalc);
Joachim Eibl's avatar
Joachim Eibl committed
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
   ++line;

   l = new QLabel(i18n("Match result:"), this);
   pGrid->addWidget(l,line,0);
   m_pAutoMergeMatchResult = new QLineEdit(this);
   m_pAutoMergeMatchResult->setReadOnly(true);
   pGrid->addWidget(m_pAutoMergeMatchResult,line,1);
   ++line;

   pGrid->addItem( new QSpacerItem(100,20), line, 0 );
   pGrid->setRowStretch( line, 5);
   ++line;

   l = new QLabel(i18n("History start regular expression:"), this);
   pGrid->addWidget(l,line,0);
Joachim Eibl's avatar
Joachim Eibl committed
473
   l->setToolTip( historyStartRegExpToolTip );
Joachim Eibl's avatar
Joachim Eibl committed
474
475
   m_pHistoryStartRegExpEdit = new QLineEdit(this);
   pGrid->addWidget(m_pHistoryStartRegExpEdit,line,1);
476
   connect(m_pHistoryStartRegExpEdit, &QLineEdit::textChanged, this, &RegExpTester::slotRecalc);
Joachim Eibl's avatar
Joachim Eibl committed
477
478
479
   ++line;

   l = new QLabel(i18n("Example history start line (with leading comment):"), this);
480
   pGrid->addWidget(l,line,0);
Joachim Eibl's avatar
Joachim Eibl committed
481
   l->setToolTip( i18n("Copy a history start line as used in your files,\n"
Joachim Eibl's avatar
Joachim Eibl committed
482
483
484
                          "including the leading comment.") );
   m_pHistoryStartExampleEdit = new QLineEdit(this);
   pGrid->addWidget(m_pHistoryStartExampleEdit,line,1);
485
   connect(m_pHistoryStartExampleEdit, &QLineEdit::textChanged, this, &RegExpTester::slotRecalc);
Joachim Eibl's avatar
Joachim Eibl committed
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
   ++line;

   l = new QLabel(i18n("Match result:"), this);
   pGrid->addWidget(l,line,0);
   m_pHistoryStartMatchResult = new QLineEdit(this);
   m_pHistoryStartMatchResult->setReadOnly(true);
   pGrid->addWidget(m_pHistoryStartMatchResult,line,1);
   ++line;

   pGrid->addItem( new QSpacerItem(100,20), line, 0 );
   pGrid->setRowStretch( line, 5);
   ++line;

   l = new QLabel(i18n("History entry start regular expression:"), this);
   pGrid->addWidget(l,line,0);
Joachim Eibl's avatar
Joachim Eibl committed
501
   l->setToolTip( historyEntryStartRegExpToolTip );
Joachim Eibl's avatar
Joachim Eibl committed
502
503
   m_pHistoryEntryStartRegExpEdit = new QLineEdit(this);
   pGrid->addWidget(m_pHistoryEntryStartRegExpEdit,line,1);
504
   connect(m_pHistoryEntryStartRegExpEdit, &QLineEdit::textChanged, this, &RegExpTester::slotRecalc);
Joachim Eibl's avatar
Joachim Eibl committed
505
506
507
508
   ++line;

   l = new QLabel(i18n("History sort key order:"), this);
   pGrid->addWidget(l,line,0);
Joachim Eibl's avatar
Joachim Eibl committed
509
   l->setToolTip( historySortKeyOrderToolTip );
Joachim Eibl's avatar
Joachim Eibl committed
510
511
   m_pHistorySortKeyOrderEdit = new QLineEdit(this);
   pGrid->addWidget(m_pHistorySortKeyOrderEdit,line,1);
512
   connect(m_pHistorySortKeyOrderEdit, &QLineEdit::textChanged, this, &RegExpTester::slotRecalc);
Joachim Eibl's avatar
Joachim Eibl committed
513
514
515
   ++line;

   l = new QLabel(i18n("Example history entry start line (without leading comment):"), this);
516
   pGrid->addWidget(l,line,0);
Joachim Eibl's avatar
Joachim Eibl committed
517
   l->setToolTip( i18n("Copy a history entry start line as used in your files,\n"
Joachim Eibl's avatar
Joachim Eibl committed
518
519
520
                          "but omit the leading comment.") );
   m_pHistoryEntryStartExampleEdit = new QLineEdit(this);
   pGrid->addWidget(m_pHistoryEntryStartExampleEdit,line,1);
521
   connect(m_pHistoryEntryStartExampleEdit, &QLineEdit::textChanged, this, &RegExpTester::slotRecalc);
Joachim Eibl's avatar
Joachim Eibl committed
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
   ++line;

   l = new QLabel(i18n("Match result:"), this);
   pGrid->addWidget(l,line,0);
   m_pHistoryEntryStartMatchResult = new QLineEdit(this);
   m_pHistoryEntryStartMatchResult->setReadOnly(true);
   pGrid->addWidget(m_pHistoryEntryStartMatchResult,line,1);
   ++line;

   l = new QLabel(i18n("Sort key result:"), this);
   pGrid->addWidget(l,line,0);
   m_pHistorySortKeyResult = new QLineEdit(this);
   m_pHistorySortKeyResult->setReadOnly(true);
   pGrid->addWidget(m_pHistorySortKeyResult,line,1);
   ++line;

   QPushButton* pButton = new QPushButton(i18n("OK"), this);
   pGrid->addWidget(pButton,line,0);
540
   connect(pButton, &QPushButton::clicked, this, &RegExpTester::accept);
Joachim Eibl's avatar
Joachim Eibl committed
541
542
543

   pButton = new QPushButton(i18n("Cancel"), this);
   pGrid->addWidget(pButton,line,1);
544
   connect(pButton, &QPushButton::clicked, this, &RegExpTester::reject);
Joachim Eibl's avatar
Joachim Eibl committed
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578

   resize( 800, sizeHint().height() );
}

void RegExpTester::init( const QString& autoMergeRegExp, const QString& historyStartRegExp, const QString& historyEntryStartRegExp, const QString historySortKeyOrder )
{
   m_pAutoMergeRegExpEdit->setText( autoMergeRegExp );
   m_pHistoryStartRegExpEdit->setText( historyStartRegExp );
   m_pHistoryEntryStartRegExpEdit->setText( historyEntryStartRegExp );
   m_pHistorySortKeyOrderEdit->setText( historySortKeyOrder );
}

QString RegExpTester::autoMergeRegExp()
{
   return m_pAutoMergeRegExpEdit->text();
}

QString RegExpTester::historyStartRegExp()
{
   return m_pHistoryStartRegExpEdit->text();
}

QString RegExpTester::historyEntryStartRegExp()
{
   return m_pHistoryEntryStartRegExpEdit->text();
}

QString RegExpTester::historySortKeyOrder()
{
   return m_pHistorySortKeyOrderEdit->text();
}

void RegExpTester::slotRecalc()
{
Joachim Eibl's avatar
Joachim Eibl committed
579
   QRegExp autoMergeRegExp( m_pAutoMergeRegExpEdit->text() );
Joachim Eibl's avatar
Joachim Eibl committed
580
581
582
583
584
585
586
587
588
   if ( autoMergeRegExp.exactMatch( m_pAutoMergeExampleEdit->text() ) )
   {
      m_pAutoMergeMatchResult->setText( i18n("Match success.") );
   }
   else
   {
      m_pAutoMergeMatchResult->setText( i18n("Match failed.") );
   }

Joachim Eibl's avatar
Joachim Eibl committed
589
   QRegExp historyStartRegExp( m_pHistoryStartRegExpEdit->text() );
Joachim Eibl's avatar
Joachim Eibl committed
590
591
592
593
594
595
596
597
598
599
600
601
602
603
   if ( historyStartRegExp.exactMatch( m_pHistoryStartExampleEdit->text() ) )
   {
      m_pHistoryStartMatchResult->setText( i18n("Match success.") );
   }
   else
   {
      m_pHistoryStartMatchResult->setText( i18n("Match failed.") );
   }


   QStringList parenthesesGroups;
   bool bSuccess = findParenthesesGroups( m_pHistoryEntryStartRegExpEdit->text(), parenthesesGroups );
   if ( ! bSuccess )
   {
Joachim Eibl's avatar
Joachim Eibl committed
604
      m_pHistoryEntryStartMatchResult->setText( i18n("Opening and closing parentheses do not match in regular expression.") );
Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
605
      m_pHistorySortKeyResult->setText( "" );
Joachim Eibl's avatar
Joachim Eibl committed
606
607
      return;
   }
Joachim Eibl's avatar
Joachim Eibl committed
608
   QRegExp historyEntryStartRegExp( m_pHistoryEntryStartRegExpEdit->text() );
Joachim Eibl's avatar
Joachim Eibl committed
609
610
611
612
613
614
615
616
617
618
619
   QString s = m_pHistoryEntryStartExampleEdit->text();

   if ( historyEntryStartRegExp.exactMatch( s ) )
   {
      m_pHistoryEntryStartMatchResult->setText( i18n("Match success.") );
      QString key = calcHistorySortKey( m_pHistorySortKeyOrderEdit->text(),historyEntryStartRegExp,parenthesesGroups);
      m_pHistorySortKeyResult->setText(key);
   }
   else
   {
      m_pHistoryEntryStartMatchResult->setText( i18n("Match failed.") );
Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
620
      m_pHistorySortKeyResult->setText( "" );
Joachim Eibl's avatar
Joachim Eibl committed
621
622
623
   }
}

Michael Reeves's avatar
Michael Reeves committed
624
#include "smalldialogs.moc"