AFChoose.cpp
1/* This file is part of the KDE project
2 Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "AFChoose.h"
21
22#include <tqlabel.h>
23#include <tqvbox.h>
24#include <tqtextstream.h>
25#include <tqdir.h>
26#include <tqwhatsthis.h>
27
28#include <tdelocale.h>
29#include <ksimpleconfig.h>
30#include <kdebug.h>
31#include <kstandarddirs.h>
32#include <kicondialog.h>
33
34#include <KPrFactory.h>
35
36AFChoose::AFChoose(TQWidget *parent, const TQString &caption, const char *name)
37 : TQTabDialog(parent,name,true)
38{
39 setCaption(caption);
40 setCancelButton(i18n("&Cancel"));
41 setOkButton(i18n("&OK"));
42 groupList.setAutoDelete(true);
43 getGroups();
44 setupTabs();
45 connect(this,TQ_SIGNAL(applyButtonPressed()),this,TQ_SLOT(chosen()));
46 connect(this,TQ_SIGNAL(cancelButtonPressed()),this,TQ_SLOT(cancelClicked()));
47}
48
49AFChoose::~AFChoose()
50{
51}
52
53void AFChoose::getGroups()
54{
55 // global autoforms (as we don't have an editor we don't have local ones)
56 TQString afDir = locate( "autoforms", ".autoforms", KPrFactory::global() );
57
58 TQFile f( afDir );
59 if ( f.open(IO_ReadOnly) ) {
60 TQTextStream t( &f );
61 TQString s;
62 while ( !t.eof() ) {
63 s = t.readLine();
64 if ( !s.isEmpty() ) {
65 grpPtr = new Group;
66 TQString directory=TQFileInfo( afDir ).dirPath() + "/" + s.simplifyWhiteSpace();
67 grpPtr->dir.setFile(directory);
68 TQDir d(directory);
69 if(d.exists(".directory")) {
70 KSimpleConfig config(d.absPath()+"/.directory", true);
71 config.setDesktopGroup();
72 grpPtr->name=config.readEntry("Name");
73 }
74 groupList.append( grpPtr );
75 }
76 }
77 f.close();
78 }
79}
80
81void AFChoose::setupTabs()
82{
83 if (!groupList.isEmpty())
84 {
85 for (grpPtr=groupList.first();grpPtr != 0;grpPtr=groupList.next())
86 {
87 grpPtr->tab = new TQVBox(this);
88 TQWhatsThis::add(grpPtr->tab, i18n( "Choose a predefined shape by clicking on it then clicking the OK button (or just double-click on the shape). You can then insert the shape onto your slide by drawing the area with the mouse pointer." ) );
89 grpPtr->loadWid = new TDEIconCanvas(grpPtr->tab);
90 // Changes for the new TDEIconCanvas (Werner)
91 TQDir d( grpPtr->dir.absFilePath() );
92 d.setNameFilter( "*.desktop" );
93 if( d.exists() ) {
94 TQStringList files=d.entryList( TQDir::Files | TQDir::Readable, TQDir::Name );
95 for(unsigned int i=0; i<files.count(); ++i) {
96 TQString path=grpPtr->dir.absFilePath() + TQChar('/');
97 files[i]=path + files[i];
98 KSimpleConfig config(files[i]);
99 config.setDesktopGroup();
100 if (config.readEntry("Type")=="Link") {
101 TQString text=config.readEntry("Name");
102 TQString icon=config.readEntry("Icon");
103 if(icon[0]!='/') // allow absolute paths for icons
104 icon=path + icon;
105 TQString filename=config.readPathEntry("URL");
106 if(filename[0]!='/') {
107 if(filename.left(6)=="file:/") // I doubt this will happen
108 filename=filename.right(filename.length()-6);
109 filename=path + filename;
110 }
111 grpPtr->entries.insert(text, filename);
112 // now load the icon and create the item
113 // This code is shamelessly borrowed from TDEIconCanvas::slotLoadFiles
114 TQImage img;
115 img.load(icon);
116 if (img.isNull()) {
117 kdWarning() << "Couldn't find icon " << icon << endl;
118 continue;
119 }
120 if (img.width() > 60 || img.height() > 60) {
121 if (img.width() > img.height()) {
122 int height = (int) ((60.0 / img.width()) * img.height());
123 img = img.smoothScale(60, height);
124 } else {
125 int width = (int) ((60.0 / img.height()) * img.width());
126 img = img.smoothScale(width, 60);
127 }
128 }
129 TQPixmap pic;
130 pic.convertFromImage(img);
131 TQIconViewItem *item = new TQIconViewItem(grpPtr->loadWid, text, pic);
132 item->setKey(text);
133 item->setDragEnabled(false);
134 item->setDropEnabled(false);
135 } else
136 continue; // Invalid .desktop file
137 }
138 }
139 grpPtr->loadWid->setBackgroundColor(colorGroup().base());
140 grpPtr->loadWid->setResizeMode(TQIconView::Adjust);
141 grpPtr->loadWid->sort();
142 connect(grpPtr->loadWid,TQ_SIGNAL(nameChanged(TQString)),
143 this,TQ_SLOT(nameChanged(TQString)));
144 connect(this, TQ_SIGNAL(currentChanged(TQWidget *)), this,
145 TQ_SLOT(tabChanged(TQWidget*)));
146 connect(grpPtr->loadWid,TQ_SIGNAL( doubleClicked ( TQIconViewItem *)),this,
147 TQ_SLOT(slotDoubleClick()));
148 grpPtr->label = new TQLabel(grpPtr->tab);
149 grpPtr->label->setText(" ");
150 grpPtr->label->setMaximumHeight(grpPtr->label->sizeHint().height());
151 addTab(grpPtr->tab,grpPtr->name);
152 }
153 }
154}
155
156void AFChoose::slotDoubleClick()
157{
158 chosen();
159 accept();
160}
161
162void AFChoose::nameChanged(TQString name)
163{
164 for (grpPtr=groupList.first();grpPtr != 0;grpPtr=groupList.next())
165 grpPtr->label->setText(name);
166}
167
168void AFChoose::tabChanged(TQWidget *w) {
169
170 for(grpPtr=groupList.first();grpPtr != 0;grpPtr=groupList.next()) {
171 if(grpPtr->tab==w)
172 grpPtr->label->setText(grpPtr->loadWid->getCurrent());
173 }
174}
175
176void AFChoose::chosen()
177{
178 if (!groupList.isEmpty())
179 {
180 for (grpPtr=groupList.first();grpPtr != 0;grpPtr=groupList.next())
181 {
182 if (grpPtr->tab->isVisible() && !grpPtr->loadWid->getCurrent().isEmpty())
183 emit formChosen(grpPtr->entries[grpPtr->loadWid->getCurrent()]);
184 else
185 emit afchooseCanceled();
186 }
187 }
188}
189
190void AFChoose::cancelClicked()
191{
192 emit afchooseCanceled();
193}
194
195void AFChoose::closeEvent ( TQCloseEvent *e )
196{
197 emit afchooseCanceled();
198 TQTabDialog::closeEvent ( e );
199}
200
201#include "AFChoose.moc"