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>
Joachim Eibl's avatar
Joachim Eibl committed
36
37
38

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

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

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

Joachim Eibl's avatar
Joachim Eibl committed
52
53
54
55
56
57
   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
58
59
60

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

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

   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
78
79
80
   m_pLineB  = new QComboBox();
   m_pLineB->setEditable(true);
   m_pLineB->insertItems( 0, m_pOptions->m_recentBFiles );
Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
81
   m_pLineB->setEditText( KUrl(n2).prettyUrl() );
Joachim Eibl's avatar
Joachim Eibl committed
82
   m_pLineB->setMinimumWidth( 200 );
Joachim Eibl's avatar
Joachim Eibl committed
83
   button   = new QPushButton( i18n("File..."), this );
84
   connect(button, &QPushButton::clicked, this, &OpenDialog::selectFileB);
Joachim Eibl's avatar
Joachim Eibl committed
85
   button2   = new QPushButton( i18n("Dir..."), this );
86
87
   connect(button2, &QPushButton::clicked, this, &OpenDialog::selectDirB);
   connect(m_pLineB, &QComboBox::editTextChanged, this, &OpenDialog::inputFilenameChanged);
Joachim Eibl's avatar
Joachim Eibl committed
88
89

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

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

   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
114
   QHBoxLayout* hl = new QHBoxLayout();
Joachim Eibl's avatar
Joachim Eibl committed
115
116
117
118
119
120
   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
121
   QMenu* m = new QMenu(this);
Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
122
123
124
125
126
127
128
129
130
   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") ));
131
   connect(m, &QMenu::triggered, this, &OpenDialog::slotSwapCopyNames);
Joachim Eibl's avatar
Joachim Eibl committed
132
   button->setMenu(m);
Joachim Eibl's avatar
Joachim Eibl committed
133
134
135
136
137


   hl->addStretch(2);

   label  = new QLabel( i18n("Output (optional):"), this );
Joachim Eibl's avatar
Joachim Eibl committed
138
139
140
   m_pLineOut = new QComboBox();
   m_pLineOut->setEditable(true);
   m_pLineOut->insertItems( 0, m_pOptions->m_recentOutputFiles );
Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
141
   m_pLineOut->setEditText( KUrl(outputName).prettyUrl() );
Joachim Eibl's avatar
Joachim Eibl committed
142
   m_pLineOut->setMinimumWidth( 200 );
Joachim Eibl's avatar
Joachim Eibl committed
143
   button = new QPushButton( i18n("File..."), this );
144
   connect(button, &QPushButton::clicked, this, &OpenDialog::selectOutputName);
Joachim Eibl's avatar
Joachim Eibl committed
145
   button2   = new QPushButton( i18n("Dir..."), this );
146
147
148
149
150
   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
151
152
153
154
155
156
157
158
159
160
161
162

   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
163
   h->addItem(new QSpacerItem(200, 0), 0, 1);
Joachim Eibl's avatar
Joachim Eibl committed
164

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

   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 );
177
   connect(button, &QPushButton::clicked, this, &OpenDialog::accept);
Joachim Eibl's avatar
Joachim Eibl committed
178
179
180
   l->addWidget( button, 1 );

   button = new QPushButton( i18n("&Cancel"), this );
181
   connect(button, &QPushButton::clicked, this, &OpenDialog::reject);
Joachim Eibl's avatar
Joachim Eibl committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
   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
201
202
203
204
205
206
   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
207
208
209
210
   if (e->type()==QEvent::Drop)
   {
      QDropEvent* d = static_cast<QDropEvent*>(e);

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

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

      if ( lst.count() > 0 )
      {
Joachim Eibl's avatar
Joachim Eibl committed
218
         static_cast<QLineEdit*>(o)->setText( QDir::toNativeSeparators( lst[0].toLocalFile() ) );
Joachim Eibl's avatar
Joachim Eibl committed
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
         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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
   
  /*KUrl newURL = bDir ? KFileDialog::getExistingDirectoryUrl( current, this)
		    : bSave ? KFileDialog::getSaveUrl( current, "all/allfiles", this)
			    : KFileDialog::getOpenUrl( current, "all/allfiles", this);*/

   KUrl 			newURL;
   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
254
255
   if ( !newURL.isEmpty() )
   {
Michael Reeves's avatar
Michael Reeves committed
256
257
258
259
260
261
        /*
        Since we are selecting a directory open in the parent directory
	not the one selected.
      */
       KFileDialog::setStartDir ( newURL.upUrl() );
       pLine->setEditText( newURL.url() );
Joachim Eibl's avatar
Joachim Eibl committed
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
   }
   // 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
288
      m_pLineOut->clearEditText();
Joachim Eibl's avatar
Joachim Eibl committed
289
290
291
   }
}

Joachim Eibl's avatar
Joachim Eibl committed
292
293
294
295
296
297
298
299
300
301
302
303
304
305
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
306
307
void OpenDialog::accept()
{
Joachim Eibl's avatar
Joachim Eibl committed
308
   int maxNofRecentFiles = 10;
Joachim Eibl's avatar
Joachim Eibl committed
309
   fixCurrentText( m_pLineA );
Joachim Eibl's avatar
Joachim Eibl committed
310
311

   QString s = m_pLineA->currentText();
Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
312
   s = QUrl::fromLocalFile(s).toLocalFile();
Joachim Eibl's avatar
Joachim Eibl committed
313
314
   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
315
   sl->removeAll(s);
Joachim Eibl's avatar
Joachim Eibl committed
316
   if ( !s.isEmpty() ) sl->prepend( s );
Joachim Eibl's avatar
Joachim Eibl committed
317
   if (sl->count()>maxNofRecentFiles) sl->erase( sl->begin()+maxNofRecentFiles, sl->end() );
Joachim Eibl's avatar
Joachim Eibl committed
318

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

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

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

   QDialog::accept();
}

Joachim Eibl's avatar
Joachim Eibl committed
346
void OpenDialog::slotSwapCopyNames( QAction* pAction ) // id selected in the popup menu
Joachim Eibl's avatar
Joachim Eibl committed
347
{
Joachim Eibl's avatar
Joachim Eibl committed
348
   int id = pAction->parentWidget()->actions().indexOf(pAction);
Joachim Eibl's avatar
Joachim Eibl committed
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
   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
367
      cb2->setEditText(t1);
Joachim Eibl's avatar
Joachim Eibl committed
368
369
      if ( id<=2 || id>=6 )
      {
Joachim Eibl's avatar
Joachim Eibl committed
370
         cb1->setEditText( t2 );
Joachim Eibl's avatar
Joachim Eibl committed
371
372
373
374
375
376
377
378
379
      }
   }
}

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

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

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

   m_pSearchString = new QLineEdit( this );
Joachim Eibl's avatar
Joachim Eibl committed
389
   layout->addWidget( m_pSearchString, line, 0, 1, 2 );
Joachim Eibl's avatar
Joachim Eibl committed
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
   ++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 );
417
   connect(pButton, &QPushButton::clicked, this, &FindDialog::accept);
Joachim Eibl's avatar
Joachim Eibl committed
418
419
420

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

   hide();
}

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

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
438
439
440
441
   setWindowTitle(i18n("Regular Expression Tester"));
   QGridLayout* pGrid = new QGridLayout( this );
   pGrid->setSpacing(5);
   pGrid->setMargin(5);
Joachim Eibl's avatar
Joachim Eibl committed
442
443
444

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

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

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

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

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

   pButton = new QPushButton(i18n("Cancel"), this);
   pGrid->addWidget(pButton,line,1);
543
   connect(pButton, &QPushButton::clicked, this, &RegExpTester::reject);
Joachim Eibl's avatar
Joachim Eibl committed
544
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

   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
578
   QRegExp autoMergeRegExp( m_pAutoMergeRegExpEdit->text() );
Joachim Eibl's avatar
Joachim Eibl committed
579
580
581
582
583
584
585
586
587
   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
588
   QRegExp historyStartRegExp( m_pHistoryStartRegExpEdit->text() );
Joachim Eibl's avatar
Joachim Eibl committed
589
590
591
592
593
594
595
596
597
598
599
600
601
602
   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
603
      m_pHistoryEntryStartMatchResult->setText( i18n("Opening and closing parentheses do not match in regular expression.") );
Joachim Eibl's avatar
0.9.93    
Joachim Eibl committed
604
      m_pHistorySortKeyResult->setText( "" );
Joachim Eibl's avatar
Joachim Eibl committed
605
606
      return;
   }
Joachim Eibl's avatar
Joachim Eibl committed
607
   QRegExp historyEntryStartRegExp( m_pHistoryEntryStartRegExpEdit->text() );
Joachim Eibl's avatar
Joachim Eibl committed
608
609
610
611
612
613
614
615
616
617
618
   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
619
      m_pHistorySortKeyResult->setText( "" );
Joachim Eibl's avatar
Joachim Eibl committed
620
621
622
   }
}

Joachim Eibl's avatar
Joachim Eibl committed
623
//#include "smalldialogs.moc"