summaryrefslogtreecommitdiffstats
path: root/kpovmodeler/pmlibrarybrowser.cpp
blob: e133f08c3198e9f57ea7b5ee5f4506c56cee314d (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
/*
**************************************************************************
                                 description
                             --------------------
    copyright            : (C) 2002 by Luis Carvalho
    email                : lpassos@mail.telepac.pt
**************************************************************************

**************************************************************************
*                                                                        *
*  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 "pmlibrarybrowser.h"

#include <qlayout.h>
#include <qlabel.h>
#include <qcombobox.h>
#include <qsplitter.h>
#include <qtimer.h>
#include <qpushbutton.h>

#include <kio/job.h>
#include <kurl.h>
#include <klocale.h>
#include <kconfig.h>
#include <kmessagebox.h>
#include <kdialog.h>
#include <kdiroperator.h>
#include <kiconloader.h>
#include <kinputdialog.h>

#include <stdlib.h>

#include "pmlineedits.h"
#include "pmdialogeditbase.h"
#include "pmlibrarymanager.h"
#include "pmlibraryhandle.h"
#include "pmlibraryobject.h"
#include "pmlibraryiconview.h"
#include "pmlibraryentrypreview.h"

// ************** PMLibraryBrowser **************************

PMLibraryBrowserView::PMLibraryBrowserView( PMPart* /*part*/, QWidget* parent /*= 0*/,
                                    const char* name /*=0*/ )
      : PMViewBase( parent, name )
{
   QHBoxLayout* hl = new QHBoxLayout( this );
   PMLibraryBrowserViewWidget* tv = new PMLibraryBrowserViewWidget( this );
   hl->addWidget( tv );
}

QString PMLibraryBrowserView::description( ) const
{
   return i18n( "Library View" );
}

PMLibraryBrowserViewWidget::PMLibraryBrowserViewWidget( QWidget* parent, const char* name )
      : QWidget( parent, name )
{
   setCaption( i18n( "Library Objects" ) );

   QVBoxLayout* vl = new QVBoxLayout( this, KDialog::spacingHint( ) );

   QHBoxLayout* hl = new QHBoxLayout( vl );

   m_pUpButton = new QPushButton( this );
   m_pUpButton->setPixmap( SmallIcon( "up" ) );
   m_pNewSubLibraryButton = new QPushButton( this );
   m_pNewSubLibraryButton->setPixmap( SmallIcon( "folder_new" ) );
   m_pNewObjectButton = new QPushButton( this );
   m_pNewObjectButton->setPixmap( SmallIcon( "filenew" ) );
   m_pDeleteObjectButton = new QPushButton( this );
   m_pDeleteObjectButton->setPixmap( SmallIcon( "editdelete" ) );
   QLabel* lbl = new QLabel( i18n( "Library: " ), this );
   m_pLibraryComboBox = new QComboBox( this );
   m_pLibraryComboBox->insertStringList( PMLibraryManager::theManager( )->availableLibraries( ) );
   m_pLibraryComboBox->setDuplicatesEnabled( false );
   m_pLibraryComboBox->setCurrentItem( 0 );
   hl->addWidget( m_pUpButton );
   hl->addWidget( m_pNewSubLibraryButton );
   hl->addWidget( m_pNewObjectButton );
   hl->addWidget( m_pDeleteObjectButton );
   hl->addWidget( lbl );
   hl->addWidget( m_pLibraryComboBox );
   hl->addStretch( 1 );

   QSplitter *splitv = new QSplitter( this );
   m_pLibraryIconView = new PMLibraryIconView( splitv );
   m_pLibraryIconView->setMinimumSize( PMDialogEditBase::previewSize( )+20, PMDialogEditBase::previewSize( ) );
   m_pLibraryEntryPreview = new PMLibraryEntryPreview( splitv );
   vl->addWidget( splitv, 99 );

   // Connect all the objects
   connect( m_pUpButton, SIGNAL( clicked( ) ), SLOT( slotUpButtonClicked( ) ) );
   connect( m_pNewSubLibraryButton, SIGNAL( clicked( ) ), SLOT( slotNewSubLibraryClicked( ) ) );
   connect( m_pNewObjectButton,  SIGNAL( clicked( ) ), SLOT( slotNewObjectClicked( ) ) );
   connect( m_pDeleteObjectButton, SIGNAL( clicked( ) ), SLOT( slotDeleteClicked( ) ) );
   connect( m_pLibraryComboBox, SIGNAL( highlighted( const QString& ) ),
                                SLOT( slotPathSelected( const QString& ) ) );
   connect( m_pLibraryIconView, SIGNAL( selectionChanged( QIconViewItem* ) ),
                                SLOT( slotSelectionChanged( QIconViewItem* ) ) );
   connect( m_pLibraryIconView, SIGNAL( executed( QIconViewItem* ) ),
                                SLOT( slotSelectionExecuted( QIconViewItem* ) ) );
   connect( m_pLibraryEntryPreview, SIGNAL( objectChanged( ) ), SLOT( slotIconViewRefresh( ) ) );

   // Set the selected library
   slotPathSelected( m_pLibraryComboBox->currentText( ) );
}

void PMLibraryBrowserViewWidget::resizeEvent( QResizeEvent* /*ev*/ )
{
}

void PMLibraryBrowserViewWidget::slotPathSelected( const QString& str )
{
   PMLibraryHandle* h = PMLibraryManager::theManager( )->getLibraryHandle( str );
   if( h )
   {
      m_pLibraryIconView->setLibrary( h );
      m_pCurrentLibrary = h;
      // This can never be a sub library
      m_pUpButton->setEnabled( false );
      // If the top library is read-only so is everthing below it
      m_topLibraryReadOnly = m_pCurrentLibrary->isReadOnly( );
      m_pNewSubLibraryButton->setEnabled( !m_topLibraryReadOnly );
      m_pNewObjectButton->setEnabled( !m_topLibraryReadOnly );
      m_pDeleteObjectButton->setEnabled( !m_topLibraryReadOnly );
   }
   else
   {
      m_pUpButton->setEnabled( false );
      m_pNewSubLibraryButton->setEnabled( false );
      m_pNewObjectButton->setEnabled( false );
      m_pDeleteObjectButton->setEnabled( false );
   }
}

void PMLibraryBrowserViewWidget::slotIconViewRefresh( )
{
   m_pLibraryIconView->refresh( );
}

void PMLibraryBrowserViewWidget::slotIconViewSetLibrary( )
{
   m_pLibraryIconView->setLibrary( m_pFutureLibrary );
   m_pCurrentLibrary = m_pFutureLibrary;
   m_pFutureLibrary = NULL;
}

void PMLibraryBrowserViewWidget::slotSelectionChanged( QIconViewItem* item )
{
   PMLibraryIconViewItem* sel = static_cast<PMLibraryIconViewItem*>( item );
   m_pLibraryIconView->setCurrentItem( sel );
   if( sel->isSubLibrary( ) )
   {
      if( m_pLibraryEntryPreview->saveIfNeeded( ) )
         QTimer::singleShot( 100, this, SLOT( slotIconViewRefresh( ) ) );
      else
         m_pLibraryEntryPreview->showPreview( sel->path( ), m_topLibraryReadOnly, true );
   }
   else
   {
      if( m_pLibraryEntryPreview->saveIfNeeded( ) )
         QTimer::singleShot( 100, this, SLOT( slotIconViewRefresh( ) ) );
      else
         m_pLibraryEntryPreview->showPreview( sel->path( ), m_topLibraryReadOnly, false );
   }
   m_pLibraryIconView->setFocus();
}

void PMLibraryBrowserViewWidget::slotSelectionExecuted( QIconViewItem* item )
{
   PMLibraryIconViewItem* sel = static_cast<PMLibraryIconViewItem*>( item );
   m_pLibraryIconView->setCurrentItem( sel );
   if( sel->isSubLibrary( ) )
   {
      // It's a sub library
      m_pFutureLibrary = new PMLibraryHandle( sel->path( ) );
      m_pLibraryEntryPreview->clearPreview( );
      QTimer::singleShot( 100, this, SLOT( slotIconViewSetLibrary( ) ) );
      m_pUpButton->setEnabled( true );
   }
   else
   {
      // It's an object
      if( m_pLibraryEntryPreview->saveIfNeeded( ) )
         QTimer::singleShot( 100, this, SLOT( slotIconViewRefresh( ) ) );
      else
         m_pLibraryEntryPreview->showPreview( sel->path( ), m_topLibraryReadOnly, false );
   }
   m_pLibraryIconView->setFocus();
}

void PMLibraryBrowserViewWidget::slotUpButtonClicked( )
{
  QDir pathManipulator( m_pCurrentLibrary->path( ) );

  pathManipulator.cdUp( );
  m_pFutureLibrary = new PMLibraryHandle( pathManipulator.path( ) );
  if( !m_pFutureLibrary->isSubLibrary( ) )
     m_pUpButton->setEnabled( false );
  QTimer::singleShot( 100, this, SLOT( slotIconViewSetLibrary( ) ) );

  // Release the current Library
  delete m_pCurrentLibrary;
}

void PMLibraryBrowserViewWidget::slotDeleteClicked( )
{
   PMLibraryIconViewItem* sel = static_cast<PMLibraryIconViewItem*>( m_pLibraryIconView->currentItem( ) );
   PMLibraryHandle::PMResult rst;
   if( !sel )
      return;
   else if( sel->isSubLibrary( ) )
      rst = m_pCurrentLibrary->deleteSubLibrary( sel->path( ) );
   else
      rst = m_pCurrentLibrary->deleteObject( sel->path( ) );

   switch( rst )
   {
      case PMLibraryHandle::Ok:
      {
         KIO::Job *job = KIO::del( sel->path() );
         connect( job, SIGNAL( result( KIO::Job * ) ), SLOT( slotJobResult( KIO::Job * ) ) );
      }
      break;
      case PMLibraryHandle::ReadOnlyLib:
         KMessageBox::error( this, i18n( "This library is read only." ), i18n( "Error" ) );
         break;
      case PMLibraryHandle::NotInLib:
         KMessageBox::error( this, i18n( "The current library does not contain that item." ), i18n( "Error" ) );
         break;
      default:
         KMessageBox::error( this, i18n( "Could not remove item." ), i18n( "Error" ) );
         break;
   }
}

void PMLibraryBrowserViewWidget::slotNewObjectClicked( )
{
   m_pLibraryEntryPreview->saveIfNeeded( );

   switch( m_pCurrentLibrary->createNewObject( ) )
   {
      case PMLibraryHandle::Ok:
         QTimer::singleShot( 100, this, SLOT( slotIconViewRefresh( ) ) );
         break;
      case PMLibraryHandle::ReadOnlyLib:
         KMessageBox::error( this, i18n( "This library is read only." ), i18n( "Error" ) );
         break;
      default:
         KMessageBox::error( this, i18n( "Could not create a new object." ), i18n( "Error" ) );
   }
}

void PMLibraryBrowserViewWidget::slotNewSubLibraryClicked( )
{
   bool result = false;

   m_pLibraryEntryPreview->saveIfNeeded( );
   QString subLibraryName = KInputDialog::getText( i18n( "Create Sub-Library" ),
                                                   i18n( "Enter the sub-library name: " ),
                                                   i18n( "Unknown" ),
                                                   &result );

   if( result )
   {
      switch( m_pCurrentLibrary->createNewSubLibrary( subLibraryName ) )
      {
         case PMLibraryHandle::Ok:
            m_pLibraryIconView->refresh( );
            break;
         case PMLibraryHandle::ExistingDir:
            KMessageBox::error( this, i18n( "That library already exists." ), i18n( "Error" ) );
            break;
         case PMLibraryHandle::ReadOnlyLib:
            KMessageBox::error( this, i18n( "This library is read only." ), i18n( "Error" ) );
            break;
         default:
            KMessageBox::error( this, i18n( "Could not create a new sub library." ), i18n( "Error" ) );
      }
   }
}

void PMLibraryBrowserViewWidget::slotJobResult( KIO::Job * job )
{
   if( job->error( ) )
      job->showErrorDialog( this );
   QTimer::singleShot( 100, this, SLOT( slotIconViewRefresh( ) ) );
}

QString PMLibraryBrowserViewFactory::description( ) const
{
   return i18n( "Library View" );
}

#include "pmlibrarybrowser.moc"