summaryrefslogtreecommitdiffstats
path: root/krename/numberdialog.cpp
blob: 661e713369a1dd8743f563b8ebaa03cd4c39c8fa (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
/***************************************************************************
                          numberdialog.cpp  -  description
                             -------------------
    begin                : Don Apr 24 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 "numberdialog.h"
#include "batchrenamer.h"

// TQt includes
#include <tqgroupbox.h>
#include <tqlayout.h>
#include <tqtooltip.h>

// KDE includes
#include <tdeapplication.h>
#include <tdeconfig.h>
#include <tdelocale.h>
#include <kpushbutton.h>
#include <tqcheckbox.h>

void KMyIntSpinBox::keyPressEvent( TQKeyEvent* e )
{
    if( e->key() == Key_Return )
        emit returnPressed();
}

NumberDialog::NumberDialog(TQValueList<int> & n,TQWidget *parent )
    : KDialogBase( KDialogBase::Plain, "KRename",
      KDialogBase::Ok | KDialogBase::Cancel, KDialogBase::Ok, parent, 0, true, true )
{
    TQSpacerItem* spacer = new TQSpacerItem( 20, 20, TQSizePolicy::Minimum, TQSizePolicy::Expanding );

    TQVBoxLayout* layout = new TQVBoxLayout( plainPage(), 6, 6 );

    TQGroupBox* group1 = new TQGroupBox( plainPage() );
    group1->setTitle( i18n("&Numbering") );
    group1->setColumnLayout(0, Qt::Vertical );
    group1->layout()->setSpacing( 6 );
    group1->layout()->setMargin( 11 );
    TQVBoxLayout* group1Layout = new TQVBoxLayout( group1->layout() );
    group1Layout->setAlignment( TQt::AlignTop );
    
    spinIndex = new KIntNumInput( group1 );
    spinIndex->setMaxValue( SPINMAX );
    spinIndex->setLabel( i18n( "Start &index:" ), AlignLeft | AlignVCenter );

    spinStep = new KIntNumInput( spinIndex, 1, group1 );
    spinStep->setRange( -SPINMAX, SPINMAX, 1, false );
    spinStep->setValue( 1 );
    spinStep->setLabel( i18n( "Step &by:" ), AlignLeft | AlignVCenter );

    checkResetCounter = new TQCheckBox( i18n("&Reset counter for every directory"), group1 );
    
    TQGroupBox* group2 = new TQGroupBox( plainPage() );
    group2->setTitle( i18n("S&kip Numbers") );
    group2->setColumnLayout(0, Qt::Horizontal );
    group2->layout()->setSpacing( 6 );
    group2->layout()->setMargin( 11 );
    TQHBoxLayout* group2Layout = new TQHBoxLayout( group2->layout() );
    group2Layout->setAlignment( TQt::AlignTop );

    listNumbers = new TDEListBox( group2 );

    buttonAdd = new KPushButton( group2 );
    buttonAdd->setText( i18n( "&Add Number" ) );

    buttonRemove = new KPushButton( group2 );
    buttonRemove->setText( i18n( "&Remove Number" ) );

    spinNumber = new KMyIntSpinBox( group2 );
    spinNumber->setMaxValue( SPINMAX );
    spinNumber->setMinValue( -SPINMAX );
    spinNumber->setValue( 0 );
    spinNumber->setFocus();

    TQVBoxLayout* layout2 = new TQVBoxLayout( 0, 6, 6 );
    layout2->addWidget( buttonAdd );
    layout2->addWidget( buttonRemove );
    layout2->addWidget( spinNumber );
    layout2->addItem( spacer );
                                                                
    group1Layout->addWidget( spinIndex );
    group1Layout->addWidget( spinStep );
    group1Layout->addWidget( checkResetCounter );
    
    group2Layout->addWidget( listNumbers );
    group2Layout->addLayout( layout2 );
    
    layout->addWidget( group1 );
    layout->addWidget( group2 );

    TQToolTip::add( spinIndex, i18n( "Number of the first file." ) );
    TQToolTip::add( spinStep, i18n( "The counter is increased/decreased by this value." ) );
    TQToolTip::add( listNumbers, i18n("Add all numbers that should be skipped by krename during the rename process.<br>"
    "E.g.: If 2 is skipped files will be numbered: file0, file1, file3, ...") );
    TQToolTip::add( checkResetCounter, i18n("<qt>The counter is set to the start index in every directory. "
                                           "This setting applies to all used counters.</qt>" ) );

    connect( buttonAdd, TQT_SIGNAL( clicked() ), this, TQT_SLOT( addNumber() ) );
    connect( buttonRemove,TQT_SIGNAL( clicked() ), this, TQT_SLOT( removeNumber() ) );
    connect( spinNumber, TQT_SIGNAL( returnPressed()), this, TQT_SLOT( addNumber() ) );
    
    for( unsigned int i = 0; i < n.count(); i++ )
            listNumbers->insertItem( TQString("%1").arg(n[i]), -1 );

    update();
}

NumberDialog::~NumberDialog()
{
}

void NumberDialog::addNumber()
{
    TQString tmp = TQString("%1").arg(spinNumber->value());
    for( unsigned int i = 0; i < listNumbers->count(); i++ )
        if( listNumbers->text(i) == tmp )
            return;
    listNumbers->insertItem( tmp, -1 );
    sort();
}

void NumberDialog::removeNumber()
{
    unsigned int i = 0;
    do {
        if(listNumbers->isSelected( i )) {
            listNumbers->removeItem( i );
            listNumbers->setSelected( i-1, true );
            return;
        } else
            i++;
    } while( i < listNumbers->count() );
}

TQValueList<int> NumberDialog::getList()
{
    TQValueList<int> skip;
    for( unsigned int i = 0; i < listNumbers->count(); i++ )
        skip.append( listNumbers->text(i).toInt() );

    return skip;
}

void NumberDialog::sort()
{
    // Not very fast, but I hope it won't be used on ot big lists ;)
    if( listNumbers->count() < 1 )
        return;

    for( unsigned int i = 0; i < listNumbers->count()-1; i++) {
        if( listNumbers->text(i).toInt() > listNumbers->text(i+1).toInt() ) {
            TQString tmp = listNumbers->text(i);
            listNumbers->removeItem(i);
            listNumbers->insertItem( tmp, i+1 );
            i = 0;
        }
    }
}

#include "numberdialog.moc"