Add (and install) svg for the new krunner interface.
[kdebase/uwolfer.git] / workspace / ksysguard / gui / Workspace.cc
bloba89c8f98694b67502b5f1b321f8d1cd048da4073
1 /*
2 KSysGuard, the KDE System Guard
4 Copyright (c) 1999 - 2002 Chris Schlaeger <cs@kde.org>
5 Copyright (c) 2006 John Tapsell <tapsell@kde.org>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License version 2 or at your option version 3 as published by
10 the Free Software Foundation.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include <QLineEdit>
24 #include <QSpinBox>
26 #include <kdebug.h>
27 #include <kfiledialog.h>
28 #include <kio/netaccess.h>
29 #include <klocale.h>
30 #include <kmessagebox.h>
31 #include <kstandarddirs.h>
32 #include <kacceleratormanager.h>
34 #include "WorkSheet.h"
35 #include "WorkSheetSettings.h"
37 #include "Workspace.h"
39 Workspace::Workspace( QWidget* parent)
40 : KTabWidget( parent )
42 KAcceleratorManager::setNoAccel(this);
43 this->setWhatsThis( i18n( "This is your work space. It holds your worksheets. You need "
44 "to create a new worksheet (Menu File->New) before "
45 "you can drag sensors here." ) );
48 Workspace::~Workspace()
52 void Workspace::saveProperties( KConfigGroup& cfg )
54 QStringList list;
55 for(int i =0; i< mSheetList.size(); i++)
56 if ( !mSheetList.at(i)->fileName().isEmpty() )
57 list.append( mSheetList.at(i)->fileName() );
59 cfg.writePathEntry( "SelectedSheets", list );
62 void Workspace::readProperties( const KConfigGroup& cfg )
64 QStringList selectedSheets = cfg.readPathEntry( "SelectedSheets", QStringList() );
65 kDebug() << "Selected Sheets = " << selectedSheets;
67 if ( selectedSheets.isEmpty() ) {
68 /* If SelectedSheets config entry is not there, then it's
69 * probably the first time the user has started KSysGuard. We
70 * then "restore" a special default configuration. */
71 selectedSheets << "ProcessTable.sgrd";
72 selectedSheets << "SystemLoad.sgrd";
73 } else if(selectedSheets[0] != "ProcessTable.sgrd") {
74 //We need to make sure that this is really is the process table on the first tab. No GUI way of changing this, but should make sure anyway.
75 //Plus this migrates users from the kde3 setup
76 selectedSheets.removeAll("ProcessTable.sgrd");
77 selectedSheets.prepend( "ProcessTable.sgrd");
80 KStandardDirs* kstd = KGlobal::dirs();
81 QString filename;
82 for ( QStringList::Iterator it = selectedSheets.begin(); it != selectedSheets.end(); ++it ) {
83 filename = kstd->findResource( "data", "ksysguard/" + *it);
84 if(!filename.isEmpty()) {
85 restoreWorkSheet( filename, false);
88 //We know that the first tab is the process table
89 setCurrentIndex(0);
90 emit currentChanged(0);
93 QString Workspace::makeNameForNewSheet() const
95 /* Find a name of the form "Sheet %d" that is not yet used by any
96 * of the existing worksheets. */
97 int i = 1;
98 bool found = false;
99 QString sheetName;
100 KStandardDirs* kstd = KGlobal::dirs();
101 do {
102 sheetName = i18n( "Sheet %1" , i++ );
103 //Check we don't have any existing files with this name
104 found = !(kstd->findResource( "data", "ksysguard/" + sheetName + ".sgrd").isEmpty());
106 //Check if we have any sheets with the same tab name or file name
107 for(int i = 0; !found && i < mSheetList.size(); i++)
108 if ( tabText(indexOf(mSheetList.at(i))) == sheetName || sheetName+".sgrd" == mSheetList.at(i)->fileName())
109 found = true;
111 } while ( found );
113 return sheetName;
116 void Workspace::newWorkSheet()
118 /* Find a name of the form "Sheet %d" that is not yet used by any
119 * of the existing worksheets. */
120 QString sheetName = makeNameForNewSheet();
122 WorkSheetSettings dlg( this, false /*not locked. New custom sheets aren't locked*/ );
123 dlg.setSheetTitle( sheetName );
124 if ( dlg.exec() ) {
125 WorkSheet* sheet = new WorkSheet( dlg.rows(), dlg.columns(), dlg.interval(), 0 );
126 sheet->setTitle( dlg.sheetTitle() );
127 sheet->setFileName( sheetName + ".sgrd" );
128 insertTab(-1, sheet, dlg.sheetTitle() );
129 mSheetList.append( sheet );
130 setCurrentIndex(indexOf( sheet ));
131 connect( sheet, SIGNAL( titleChanged( QWidget* ) ),
132 SLOT( updateSheetTitle( QWidget* )));
136 void Workspace::updateSheetTitle( QWidget* wdg )
138 kDebug() << "update sheet title";
139 if ( wdg )
140 setTabText( indexOf(wdg), static_cast<WorkSheet*>( wdg )->translatedTitle() );
143 bool Workspace::saveOnQuit()
145 kDebug() << "In saveOnQuit()";
146 for(int i = 0; i < mSheetList.size(); i++) {
147 if ( mSheetList.at(i)->fileName().isEmpty() ) {
148 int res = KMessageBox::warningYesNoCancel( this,
149 i18n( "The worksheet '%1' contains unsaved data.\n"
150 "Do you want to save the worksheet?",
151 tabText(indexOf( mSheetList.at(i) )) ), QString(), KStandardGuiItem::save(), KStandardGuiItem::discard() );
152 if ( res == KMessageBox::Yes )
153 saveWorkSheet( mSheetList.at(i) );
154 else if ( res == KMessageBox::Cancel )
155 return false; // abort quit
156 } else
157 saveWorkSheet(mSheetList.at(i));
159 return true;
162 void Workspace::importWorkSheet()
164 KUrl url = KFileDialog::getOpenUrl( QString(), i18n("*.sgrd|Sensor Files"), this, i18n( "Select Worksheet to Import" ) );
166 importWorkSheet( url );
169 void Workspace::importWorkSheet( const KUrl &url )
171 if ( url.isEmpty() )
172 return;
174 /* It's probably not worth the effort to make this really network
175 * transparent. Unless s/o beats me up I use this pseudo transparent
176 * code. */
177 QString tmpFile;
178 KIO::NetAccess::download( url, tmpFile, this );
180 // Import sheet from file.
181 if ( !restoreWorkSheet( tmpFile ) )
182 return;
184 mSheetList.last()->setFileName( makeNameForNewSheet() + ".sgrd");
186 KIO::NetAccess::removeTempFile( tmpFile );
189 bool Workspace::saveWorkSheet( WorkSheet *sheet )
191 if ( !sheet ) {
192 KMessageBox::sorry( this, i18n( "You do not have a worksheet that could be saved." ) );
193 return false;
196 KStandardDirs* kstd = KGlobal::dirs();
197 QString fileName = kstd->saveLocation( "data", "ksysguard") + sheet->fileName();
199 if ( !sheet->save( fileName ) ) {
200 return false;
202 return true;
205 void Workspace::exportWorkSheet()
207 exportWorkSheet( (WorkSheet*)currentWidget() );
210 void Workspace::exportWorkSheet( WorkSheet *sheet )
212 if ( !sheet ) {
213 KMessageBox::sorry( this, i18n( "You do not have a worksheet that could be saved." ) );
214 return;
217 QString fileName;
218 do {
219 fileName = KFileDialog::getSaveFileName( tabText(indexOf( currentWidget() ))+ ".sgrd",
220 "*.sgrd", this, i18n("Export Work Sheet") );
221 if ( fileName.isEmpty() )
222 return;
224 } while ( !sheet->exportWorkSheet( fileName ) );
228 void Workspace::removeWorkSheet()
230 WorkSheet *current = (WorkSheet*)currentWidget();
232 if ( current ) {
233 saveWorkSheet( current );
235 removeTab(indexOf( current ));
236 mSheetList.removeAll( current );
237 } else {
238 QString msg = i18n( "There are no worksheets that could be deleted." );
239 KMessageBox::error( this, msg );
243 void Workspace::removeAllWorkSheets()
245 WorkSheet *sheet;
246 while ( ( sheet = (WorkSheet*)currentWidget() ) != 0 ) {
247 saveWorkSheet( sheet );
248 removeTab(indexOf( sheet ));
249 mSheetList.removeAll( sheet );
250 delete sheet;
254 void Workspace::removeWorkSheet( const QString &fileName )
256 for(int i = 0; i < mSheetList.size(); i++) {
257 WorkSheet *sheet = mSheetList.at(i);
258 if ( sheet->fileName() == fileName ) {
259 removeTab(indexOf( sheet ));
260 mSheetList.removeAt( i );
261 delete sheet;
262 return;
267 bool Workspace::restoreWorkSheet( const QString &fileName, bool switchToTab)
269 // extract filename without path
270 QString baseName = fileName.right( fileName.length() - fileName.lastIndexOf( '/' ) - 1 );
272 WorkSheet *sheet = new WorkSheet( 0 );
273 sheet->setFileName( baseName );
274 if ( !sheet->load( fileName ) ) {
275 delete sheet;
276 return false;
278 mSheetList.append( sheet );
280 connect( sheet, SIGNAL( titleChanged( QWidget* ) ),
281 SLOT( updateSheetTitle( QWidget* )));
283 insertTab(-1, sheet, sheet->translatedTitle() );
284 if(switchToTab)
285 setCurrentIndex(indexOf(sheet));
287 return true;
290 void Workspace::cut()
292 WorkSheet *current = (WorkSheet*)currentWidget();
294 if ( current )
295 current->cut();
298 void Workspace::copy()
300 WorkSheet *current = (WorkSheet*)currentWidget();
302 if ( current )
303 current->copy();
306 void Workspace::paste()
308 WorkSheet *current = (WorkSheet*)currentWidget();
310 if ( current )
311 current->paste();
314 void Workspace::configure()
316 WorkSheet *current = (WorkSheet*)currentWidget();
318 if ( !current )
319 return;
321 current->settings();
324 void Workspace::applyStyle()
326 if ( currentWidget() )
327 ((WorkSheet*)currentWidget())->applyStyle();
330 #include "Workspace.moc"