summaryrefslogtreecommitdiffstats
path: root/krename/coorddialog.cpp
blob: 959c96974c1303f2aa39271224c64041b8638f7e (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
/***************************************************************************
                          coorddialog.cpp  -  description
                             -------------------
    begin                : Die Feb 4 2003
    copyright            : (C) 2003 by Dominik Seichter
    email                : domseichter@web.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 "coorddialog.h"

// TQt includes
#include <tqcheckbox.h>
#include <tqfontmetrics.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqvalidator.h>

// KDE includes
#include <tdeapplication.h>                                                                  
#include <tdelocale.h>

DSLineEdit::DSLineEdit( TQWidget* parent, const char* name )
    : KLineEdit( parent, name )
{
}

void DSLineEdit::keyPressEvent( TQKeyEvent* e )
{
    KLineEdit::keyPressEvent( e );
    emit changed();
}

void DSLineEdit::mousePressEvent( TQMouseEvent* e )
{
    KLineEdit::mousePressEvent( e );
    emit changed();
}

bool CoordDialog::m_inversion = false;

CoordDialog::CoordDialog( const TQString & file, TQWidget *_parent, const char *name )
    : KDialogBase( KDialogBase::Plain, "KRename",
      KDialogBase::Ok | KDialogBase::Cancel, KDialogBase::Ok, _parent, name, true, true ), m_file( file )
{
    TQFrame* parent = plainPage();
    TQVBoxLayout* layout = new TQVBoxLayout( parent );

    filename = new DSLineEdit( parent );
    filename->setText( file );
    filename->setValidator( new TQRegExpValidator( TQRegExp( file ), TQT_TQOBJECT(this) ) );
    
    preview = new TQLabel( parent );

    checkInvert = new TQCheckBox( i18n("&Invert selection"), plainPage() );
    checkInvert->setChecked( m_inversion );
    
    layout->addWidget( new TQLabel( i18n("Please select the text you want to insert:"), plainPage() ) );
    layout->addWidget( filename );
    layout->addWidget( checkInvert );
    layout->addWidget( preview );

    updateCommand();
    connect( filename, TQT_SIGNAL( selectionChanged() ), this, TQT_SLOT( updateCommand() ) );
    connect( checkInvert, TQT_SIGNAL( clicked() ), this, TQT_SLOT( updateCommand() ) );
    connect( filename, TQT_SIGNAL( textChanged( const TQString & ) ), this, TQT_SLOT( resetText() ) );
    connect( filename, TQT_SIGNAL( changed() ), this, TQT_SLOT( updateCommand() ) );

    show();
    
    TQFontMetrics fm( filename->font() );
    int w = fm.width( file );
    if( w > width() )
        resize(
            ( w < TDEApplication::desktop()->width() - 40 ? w + 40 : TDEApplication::desktop()->width() ), height() );
}

CoordDialog::~CoordDialog()
{
}

void CoordDialog::updateCommand()
{
    int start = 0;
    int end = 0;
    m_command = "";    

    (void)filename->getSelection( &start, &end );

    if( !filename->text().isEmpty() ) {
       if( checkInvert->isChecked() && filename->hasSelectedText() ) {
            // inverted
            if( end ) {
                start++;
                end++;
                if( start > 1 )
                    m_command = TQString("[$1;%1]").arg(start-1);

                if( end <= (signed int)filename->text().length() )
                    m_command.append( TQString("[$%1-[length]]").arg(end) );
            } 
        } else if( checkInvert->isChecked() && !filename->hasSelectedText() ) {
            int p = filename->cursorPosition();
            m_command = TQString("[$1;%1][$%2-[length]]").arg(p).arg(p+1);            
        } else if( !checkInvert->isChecked() && filename->hasSelectedText() ){
            if( end ) {
                start++;
                end++;
                if( end <= (signed int)filename->text().length() )
                    m_command = TQString("[$%1;%2]").arg(start).arg(end-start);
                else
                    m_command = TQString("[$%1-[length]]").arg(start);
            }
        } else if( !checkInvert->isChecked() && !filename->hasSelectedText() ) {
            int p = filename->cursorPosition();
            m_command = TQString("[$%1-[length]]").arg( p );
        }

    }
    
    preview->setText( i18n("Preview: ") + m_command );
}

void CoordDialog::resetText()
{
    filename->setText( m_file );
    updateCommand();
}

TQString CoordDialog::coords()
{
    m_inversion = checkInvert->isChecked();
    return m_command;
}