summaryrefslogtreecommitdiffstats
path: root/tdemid/collectdlg.cpp
blob: 20ac8def7cc4adfdfdafc6d54c50a3027b7cdc29 (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
/**************************************************************************

    collectdlg.cpp  - The collections manager dialog
    Copyright (C) 1998  Antonio Larrosa Jimenez

    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 Street, Fifth Floor, Boston, MA  02110-1301, USA.

    Send comments and bug fixes to larrosa@kde.org
    or to Antonio Larrosa, Rio Arnoya, 10 5B, 29006 Malaga, Spain

***************************************************************************/
#include <tqpushbutton.h>
#include <tqlistbox.h>
#include <tqlabel.h>

#include <kapplication.h>
#include <kfiledialog.h>
#include <klocale.h>
#include <kinputdialog.h>
#include <kmessagebox.h>
#include <kurl.h>
#include <kpushbutton.h>
#include <kstdguiitem.h>
#include "collectdlg.h"
#include "version.h"
#include "songlist.h"
#include "slman.h"

CollectionDialog::CollectionDialog(SLManager *slm,int selC,TQWidget *parent,const char *name) : TQDialog(parent,name,TRUE)
{
setCaption(i18n("Collections Manager"));
ok=new KPushButton(KStdGuiItem::ok(),this);
ok->setGeometry(140,200,100,30);
connect(ok,TQT_SIGNAL(clicked()),TQT_SLOT(accept()) );
cancel=new KPushButton(KStdGuiItem::cancel(),this);
cancel->setGeometry(250,200,100,30);
connect(cancel,TQT_SIGNAL(clicked()),TQT_SLOT(reject()) );

label=new TQLabel(i18n("Available collections:"),this);
label->adjustSize();
label->move(10,10);
collections=new TQListBox(this,"collectionlist");
collections->setGeometry(10,20+label->height(),340,90);
connect(collections,TQT_SIGNAL(highlighted(int)),TQT_SLOT(collectionselected(int)));
connect(collections,TQT_SIGNAL(selected(int)),TQT_SLOT(changeCollectionName(int)));
slman=slm;
for (int i=0;i<=slman->numberOfCollections();i++)
    {
    collections->insertItem(i18n( slman->getCollectionName(i) ),i);
#ifdef COLLECTDLGDEBUG
    printf("Name : %s\n",slman->getCollectionName(i));
#endif
    };
selectedC=selC;
#ifdef COLLECTDLGDEBUG
printf("selectedC : %d\n",selectedC);
#endif

label2=new TQLabel(i18n("Songs in selected collection:"),this);
label2->adjustSize();
label2->move(10,collections->y()+collections->height()+10);

songs=new TQListBox(this,"songlist");
songs->setGeometry(10,label2->y()+label2->height()+10,340,120);
connect(songs,TQT_SIGNAL(highlighted(int)),TQT_SLOT(songselected(int)));
currentsl=slman->getCollection(selectedC);
if (slman->numberOfCollections()>0)
    {
    collections->setCurrentItem(selectedC);
    collections->centerCurrentItem();
    };
//fillInSongList();
newC=new TQPushButton(i18n("&New..."),this);
newC->adjustSize();
newC->move(360,collections->y()+5);
connect(newC,TQT_SIGNAL(clicked()),TQT_SLOT(newCollection()) );
copyC=new TQPushButton(i18n("&Copy..."),this);
copyC->adjustSize();
copyC->move(360,newC->y()+newC->height()+5);
connect(copyC,TQT_SIGNAL(clicked()),TQT_SLOT(copyCollection()) );
deleteC=new TQPushButton(i18n("Delete"),this);
deleteC->adjustSize();
deleteC->move(360,copyC->y()+copyC->height()+5);
connect(deleteC,TQT_SIGNAL(clicked()),TQT_SLOT(deleteCollection()) );

addS=new TQPushButton(i18n("&Add..."),this);
addS->adjustSize();
addS->move(360,songs->y()+5);
connect(addS,TQT_SIGNAL(clicked()),TQT_SLOT(addSong()) );
delS=new TQPushButton(i18n("&Remove"),this);
delS->adjustSize();
delS->move(360,addS->y()+addS->height()+5);
connect(delS,TQT_SIGNAL(clicked()),TQT_SLOT(removeSong()) );

ok->move(ok->x(),songs->y()+songs->height()+10);
cancel->move(ok->x()+ok->width()+5,ok->y());

setMinimumSize(400,ok->y()+ok->height()+5);
//setMaximumSize(360,240);
}

void CollectionDialog::collectionselected(int idx)
{
selectedC=idx;
#ifdef COLLECTDLGDEBUG
printf("Selected collection: %d\n",selectedC);
#endif
currentsl=slman->getCollection(selectedC);
fillInSongList();
}

void CollectionDialog::fillInSongList(void)
{
TQString qs;
songs->clear();
if (currentsl!=NULL)
   {
   currentsl->iteratorStart();
   int i=0;
   while (!currentsl->iteratorAtEnd())
       {
       qs=TQString(currentsl->getIteratorName());
       //KURL::decode(qs);
       songs->insertItem(qs,i);
       currentsl->iteratorNext();
       i++;
       };
   songs->setCurrentItem(currentsl->getActiveSongID()-1);
   songs->centerCurrentItem();
   };
}

void CollectionDialog::songselected(int idx)
{
selectedS=idx;
currentsl->setActiveSong(idx+1);

#ifdef COLLECTDLGDEBUG
printf("Selected song: %d\n",selectedS);
#endif
}

void CollectionDialog::newCollection()
{
  bool ok;
  TQString name = KInputDialog::getText( i18n( "New Collection" ),
      i18n( "Enter the name of the new collection:" ), TQString(),
      &ok, this );

  if (ok)
  {
   int i=slman->createCollection(name.ascii());
   if (i==-1)
	{
	TQString s = i18n("The name '%1' is already used").arg(name);
	KMessageBox::sorry(this, s);
	}
	else
	{
	collections->insertItem(name,i);
	collections->setCurrentItem(i);
        collections->centerCurrentItem();
	};
   };
}

void CollectionDialog::copyCollection()
{
SongList *src=currentsl;
int i;
  bool ok;
  TQString name = KInputDialog::getText( i18n( "Copy Collection" ),
      i18n( "Enter the name of the copy collection:" ), TQString(),
      &ok, this );

  if (ok)
  {
   i=slman->createCollection(name.ascii());
   if (i==-1)
	{
	TQString s = i18n("The name '%1' is already used").arg(name);
	KMessageBox::sorry(this, s);
	}
	else
	{
	collections->insertItem(name,i);
	SongList *tgt=slman->getCollection(i);
	src->iteratorStart();
	while (!src->iteratorAtEnd())
	   {
	   tgt->AddSong(src->getIteratorName());
	   src->iteratorNext();
	   };
	collections->setCurrentItem(i);
        collections->centerCurrentItem();
	};
   };
}

void CollectionDialog::deleteCollection()
{
if (selectedC==0) return;
slman->deleteCollection(selectedC);
int i=selectedC;
collections->removeItem(selectedC);
collections->setCurrentItem(i);
collections->centerCurrentItem();
}

void CollectionDialog::changeCollectionName(int idx)
{
if (idx==0) return;
  bool ok;

  TQString name = KInputDialog::getText( i18n( "Change Collection Name" ),
      i18n( "Enter the name of the selected collection:" ), TQString(),
      &ok, this );

  if (ok)
  {
   if (slman->getCollection(name.ascii())!=NULL)
	{
	TQString s = i18n("The name '%1' is already used").arg(name);
	KMessageBox::sorry(this, s);
	}
	else
	{
	slman->changeCollectionName(idx,name.ascii());
	collections->changeItem(name,idx);
	};
   };
}

void CollectionDialog::addSong(const KURL &url)
{
  if (currentsl==NULL) return;
  if( url.isEmpty()) return;

  int id=currentsl->AddSong(TQFile::encodeName(url.path()));

  if (id==-1)
  {
    printf("Couldn't add song to collection\n");
    return;
  };

  songs->insertItem(url.fileName(),id-1);
}


void CollectionDialog::addSong()
{
  KURL::List urls = KFileDialog::getOpenURLs(":MidiFiles",
      "*.kar *.mid *.kar.gz *.mid.gz\n*.kar *.kar.gz\n*.mid *.mid.gz\n*",this);

  KURL::List::Iterator it;
  for( it = urls.begin(); it != urls.end(); ++it )
	addSong(*it);

}

void CollectionDialog::removeSong()
{
if (currentsl==NULL) return;
currentsl->DelSong(selectedS+1);
int i=selectedS;
songs->removeItem(selectedS);
songs->setCurrentItem(i);
}

void CollectionDialog::resizeEvent(TQResizeEvent *)
{
int maxw=newC->width();
if (copyC->width()>maxw) maxw=copyC->width();
if (deleteC->width()>maxw) maxw=deleteC->width();
if (addS->width()>maxw) maxw=addS->width();
if (delS->width()>maxw) maxw=delS->width();

newC->setGeometry(width()-maxw-5,newC->y(),maxw,newC->height());
copyC->setGeometry(width()-maxw-5,copyC->y(),maxw,copyC->height());
deleteC->setGeometry(width()-maxw-5,deleteC->y(),maxw,deleteC->height());

collections->resize(width()-maxw-20,(height()*35)/100);
label2->move(10,collections->y()+collections->height()+10);
songs->setGeometry(10,label2->y()+label2->height()+10,width()-maxw-20,height()-(label2->y()+label2->height()+10+ok->height()+10));
addS->setGeometry(width()-maxw-5,songs->y()+5,maxw,addS->height());
delS->setGeometry(width()-maxw-5,addS->y()+addS->height()+5,maxw,delS->height());

cancel->move(width()-cancel->width()-5,height()-cancel->height()-5);
ok->move(cancel->x()-5-ok->width(),height()-ok->height()-5);
}

int CollectionDialog::selectedC=0;
int CollectionDialog::selectedS=0;
#include "collectdlg.moc"