add more spacing
[personal-kdebase.git] / workspace / ksysguard / gui / Workspace.cc
blob62e77f9677e799f5a28ed1ed9a4451cdb569ed66
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>
33 #include <kactioncollection.h>
34 #include <kmenu.h>
35 #include <knewstuff2/engine.h>
37 #include "WorkSheet.h"
38 #include "WorkSheetSettings.h"
40 #include "Workspace.h"
41 #include "ksysguard.h"
43 Workspace::Workspace( QWidget* parent)
44 : KTabWidget( parent )
46 KAcceleratorManager::setNoAccel(this);
47 this->setWhatsThis( i18n( "This is your work space. It holds your tabs. You need "
48 "to create a new tab (Menu File->New) before "
49 "you can drag sensors here." ) );
50 setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
53 Workspace::~Workspace()
57 void Workspace::saveProperties( KConfigGroup& cfg )
59 QStringList list;
60 for(int i =0; i< mSheetList.size(); i++)
61 if ( !mSheetList.at(i)->fileName().isEmpty() )
62 list.append( mSheetList.at(i)->fileName() );
64 cfg.writePathEntry( "SelectedSheets", list );
67 void Workspace::readProperties( const KConfigGroup& cfg )
69 QStringList selectedSheets = cfg.readPathEntry( "SelectedSheets", QStringList() );
70 kDebug() << "Selected Sheets = " << selectedSheets;
72 if ( selectedSheets.isEmpty() ) {
73 /* If SelectedSheets config entry is not there, then it's
74 * probably the first time the user has started KSysGuard. We
75 * then "restore" a special default configuration. */
76 selectedSheets << "ProcessTable.sgrd";
77 selectedSheets << "SystemLoad.sgrd";
78 } else if(selectedSheets[0] != "ProcessTable.sgrd") {
79 //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.
80 //Plus this migrates users from the kde3 setup
81 selectedSheets.removeAll("ProcessTable.sgrd");
82 selectedSheets.prepend( "ProcessTable.sgrd");
85 KStandardDirs* kstd = KGlobal::dirs();
86 QString filename;
87 for ( QStringList::Iterator it = selectedSheets.begin(); it != selectedSheets.end(); ++it ) {
88 filename = kstd->findResource( "data", "ksysguard/" + *it);
89 if(!filename.isEmpty()) {
90 restoreWorkSheet( filename, false);
93 //We know that the first tab is the process table
94 setCurrentIndex(0);
95 emit currentChanged(0);
98 QString Workspace::makeNameForNewSheet() const
100 /* Find a name of the form "Sheet %d" that is not yet used by any
101 * of the existing worksheets. */
102 int i = 1;
103 bool found = false;
104 QString sheetName;
105 KStandardDirs* kstd = KGlobal::dirs();
106 do {
107 sheetName = i18n( "Sheet %1" , i++ );
108 //Check we don't have any existing files with this name
109 found = !(kstd->findResource( "data", "ksysguard/" + sheetName + ".sgrd").isEmpty());
111 //Check if we have any sheets with the same tab name or file name
112 for(int i = 0; !found && i < mSheetList.size(); i++)
113 if ( tabText(indexOf(mSheetList.at(i))) == sheetName || sheetName+".sgrd" == mSheetList.at(i)->fileName())
114 found = true;
116 } while ( found );
118 return sheetName;
121 void Workspace::newWorkSheet()
123 /* Find a name of the form "Sheet %d" that is not yet used by any
124 * of the existing worksheets. */
125 QString sheetName = makeNameForNewSheet();
127 WorkSheetSettings dlg( this, false /*not locked. New custom sheets aren't locked*/ );
128 dlg.setSheetTitle( sheetName );
129 if ( dlg.exec() ) {
130 WorkSheet* sheet = new WorkSheet( dlg.rows(), dlg.columns(), dlg.interval(), 0 );
131 sheet->setTitle( dlg.sheetTitle() );
132 sheet->setFileName( sheetName + ".sgrd" );
133 insertTab(-1, sheet, dlg.sheetTitle() );
134 mSheetList.append( sheet );
135 setCurrentIndex(indexOf( sheet ));
136 connect( sheet, SIGNAL( titleChanged( QWidget* ) ),
137 SLOT( updateSheetTitle( QWidget* )));
140 void Workspace::contextMenu (int index, const QPoint &point) {
141 KMenu pm;
143 // QAction *new_worksheet = pm.addAction( Toplevel->actionCollection()->action("new_worksheet") );
145 // QAction *action = pm.exec( point );
149 void Workspace::updateSheetTitle( QWidget* wdg )
151 kDebug() << "update sheet title";
152 if ( wdg )
153 setTabText( indexOf(wdg), static_cast<WorkSheet*>( wdg )->translatedTitle() );
156 bool Workspace::saveOnQuit()
158 kDebug() << "In saveOnQuit()";
159 for(int i = 0; i < mSheetList.size(); i++) {
160 if ( mSheetList.at(i)->fileName().isEmpty() ) {
161 int res = KMessageBox::warningYesNoCancel( this,
162 i18n( "The tab '%1' contains unsaved data.\n"
163 "Do you want to save the tab?",
164 tabText(indexOf( mSheetList.at(i) )) ), QString(), KStandardGuiItem::save(), KStandardGuiItem::discard() );
165 if ( res == KMessageBox::Yes )
166 saveWorkSheet( mSheetList.at(i) );
167 else if ( res == KMessageBox::Cancel )
168 return false; // abort quit
169 } else
170 saveWorkSheet(mSheetList.at(i));
172 return true;
175 void Workspace::importWorkSheet()
177 KUrl url = KFileDialog::getOpenUrl( QString(), i18n("*.sgrd|Sensor Files (*.sgrd)"), this, i18n( "Select Tab File to Import" ) );
179 importWorkSheet( url );
182 void Workspace::importWorkSheet( const KUrl &url )
184 if ( url.isEmpty() )
185 return;
187 /* It's probably not worth the effort to make this really network
188 * transparent. Unless s/o beats me up I use this pseudo transparent
189 * code. */
190 QString tmpFile;
191 KIO::NetAccess::download( url, tmpFile, this );
193 // Import sheet from file.
194 if ( !restoreWorkSheet( tmpFile ) )
195 return;
197 mSheetList.last()->setFileName( makeNameForNewSheet() + ".sgrd");
199 KIO::NetAccess::removeTempFile( tmpFile );
202 bool Workspace::saveWorkSheet( WorkSheet *sheet )
204 if ( !sheet ) {
205 KMessageBox::sorry( this, i18n( "You do not have a tab that could be saved." ) );
206 return false;
209 KStandardDirs* kstd = KGlobal::dirs();
210 QString fileName = kstd->saveLocation( "data", "ksysguard") + sheet->fileName();
212 if ( !sheet->save( fileName ) ) {
213 return false;
215 return true;
218 void Workspace::exportWorkSheet()
220 exportWorkSheet( (WorkSheet*)currentWidget() );
223 void Workspace::exportWorkSheet( WorkSheet *sheet )
225 if ( !sheet ) {
226 KMessageBox::sorry( this, i18n( "You do not have a tab that could be saved." ) );
227 return;
230 QString fileName;
231 do {
232 fileName = KFileDialog::getSaveFileName( tabText(indexOf( currentWidget() ))+ ".sgrd",
233 "*.sgrd", this, i18n("Export Tab") );
234 if ( fileName.isEmpty() )
235 return;
237 } while ( !sheet->exportWorkSheet( fileName ) );
241 void Workspace::removeWorkSheet()
243 WorkSheet *current = (WorkSheet*)currentWidget();
245 if ( current ) {
246 saveWorkSheet( current );
248 removeTab(indexOf( current ));
249 mSheetList.removeAll( current );
250 } else {
251 QString msg = i18n( "There are no tabs that could be deleted." );
252 KMessageBox::error( this, msg );
256 void Workspace::removeAllWorkSheets()
258 WorkSheet *sheet;
259 while ( ( sheet = (WorkSheet*)currentWidget() ) != 0 ) {
260 saveWorkSheet( sheet );
261 removeTab(indexOf( sheet ));
262 mSheetList.removeAll( sheet );
263 delete sheet;
267 void Workspace::removeWorkSheet( const QString &fileName )
269 for(int i = 0; i < mSheetList.size(); i++) {
270 WorkSheet *sheet = mSheetList.at(i);
271 if ( sheet->fileName() == fileName ) {
272 removeTab(indexOf( sheet ));
273 mSheetList.removeAt( i );
274 delete sheet;
275 return;
280 void Workspace::getHotNewWorksheet()
282 kDebug() << "Need to handle get new stuff";
284 KNS::Engine engine(this);
285 if(engine.init("ksysguard.knsrc"))
287 KNS::Entry::List entries = engine.downloadDialogModal(this);
288 //TODO: inspect entries here
289 //Don't qDeleteAll entry it's already done when engine is deleted otherwise double delete
290 //qDeleteAll(entries);
294 bool Workspace::restoreWorkSheet( const QString &fileName, bool switchToTab)
296 // extract filename without path
297 QString baseName = fileName.right( fileName.length() - fileName.lastIndexOf( '/' ) - 1 );
299 foreach( WorkSheet *sheet, mSheetList ) {
300 if(sheet->fileName() == baseName)
301 return false; //Don't add the same sheet twice
304 WorkSheet *sheet = new WorkSheet( 0 );
305 sheet->setFileName( baseName );
306 if ( !sheet->load( fileName ) ) {
307 delete sheet;
308 return false;
310 mSheetList.append( sheet );
312 connect( sheet, SIGNAL( titleChanged( QWidget* ) ),
313 SLOT( updateSheetTitle( QWidget* )));
315 insertTab(-1, sheet, sheet->translatedTitle() );
316 if(switchToTab)
317 setCurrentIndex(indexOf(sheet));
319 return true;
322 void Workspace::cut()
324 WorkSheet *current = (WorkSheet*)currentWidget();
326 if ( current )
327 current->cut();
330 void Workspace::copy()
332 WorkSheet *current = (WorkSheet*)currentWidget();
334 if ( current )
335 current->copy();
338 void Workspace::paste()
340 WorkSheet *current = (WorkSheet*)currentWidget();
342 if ( current )
343 current->paste();
346 void Workspace::configure()
348 WorkSheet *current = (WorkSheet*)currentWidget();
350 if ( !current )
351 return;
353 current->settings();
356 void Workspace::applyStyle()
358 if ( currentWidget() )
359 ((WorkSheet*)currentWidget())->applyStyle();
362 #include "Workspace.moc"