File: pExtendedWorkspace.cpp

package info (click to toggle)
monkeystudio 1.9.0.4%2Bgit20161218-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 41,500 kB
  • ctags: 22,118
  • sloc: cpp: 144,671; ansic: 33,969; python: 2,922; makefile: 127; sh: 122; php: 73; cs: 69
file content (591 lines) | stat: -rw-r--r-- 16,291 bytes parent folder | download | duplicates (3)
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/****************************************************************************
    Copyright (C) 2005 - 2011  Filipe AZEVEDO & The Monkey Studio Team
    http://monkeystudio.org licensing under the GNU GPL.

    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 St, Fifth Floor, Boston, MA  02110-1301  USA
****************************************************************************/
#include "pExtendedWorkspace.h"
#include "pFilesListWidget.h"

#include <QBoxLayout>
#include <QMdiArea>
#include <QMdiSubWindow>
#include <QIcon>
#include <QCloseEvent>
#include <QFile>
#include <QMenu>
#include <QMainWindow>
#include <QStackedLayout>
#include <QStackedWidget>
#include <Qt>
#include <QApplication>

/*!
    \details Create a new pExtendedWorkspace instance
    \param parent The parent widget
    \param mode The current mode
*/
pExtendedWorkspace::pExtendedWorkspace( QWidget* parent, pExtendedWorkspace::DocumentMode mode )
    : QWidget( parent )
{
    mLastDocument = 0;
    // tab widget
    mTabLayout = new QBoxLayout( QBoxLayout::LeftToRight );
    mTabLayout->setSpacing( 3 );
    mTabLayout->setMargin( 0 );
    //mTabBar = new pTabBar (this);
    //mTabLayout->addWidget( ( mTabBar ) );

    mFilesList = new pFilesListWidget( tr( "Files List"), this );
    
    // document widget
    mStackedLayout = new QStackedLayout;
    mStackedLayout->addWidget( ( mStackedWidget = new QStackedWidget ) );
    mStackedLayout->addWidget( ( mMdiAreaWidget = new QMdiArea ) );

    // main layout
    mLayout = new QBoxLayout( QBoxLayout::TopToBottom, this );
    mLayout->setSpacing( 0 );
    mLayout->setMargin( 0 );
    mLayout->addLayout( mTabLayout );
    mLayout->addLayout( mStackedLayout );
    
    // init view
    //mTabBar->setDrawBase( false );
    //mTabBar->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred ) );
    
    mDocMode = (DocumentMode) -1; //for avoid return on start method setDocMode (m)
    setDocMode( mode );
    
    connect( mMdiAreaWidget, SIGNAL( subWindowActivated( QMdiSubWindow* ) ), this, SLOT( mdiArea_subWindowActivated( QMdiSubWindow* ) ) );
    connect( this, SIGNAL( currentChanged( int ) ), this, SLOT( internal_currentChanged( int ) ) );
}

pExtendedWorkspace::~pExtendedWorkspace()
{
    // close all document
    closeAllDocuments();

    // delete container
    delete mMdiAreaWidget;
}

bool pExtendedWorkspace::eventFilter( QObject* object, QEvent* event )
{
    // get document
    if ( QWidget* td = qobject_cast<QWidget*>( object ) )
    {
        if ( td->inherits( "QMdiSubWindow" ) && event->type() == QEvent::Close )
        {
            event->ignore();
            closeCurrentDocument();
            return true;
        }
        else
        {
            if ( indexOf( td ) == -1 )
                return QWidget::eventFilter( object, event );
            
            // get event type
            QEvent::Type type = event->type();
            
            // child modified state
            switch ( type )
            {
            case QEvent::ModifiedChange:
                emit modifiedChanged( indexOf( td ), td->isWindowModified() );
                break;
            case QEvent::Close:
                closeDocument( td );
                return true;
                break;
            case QEvent::WindowActivate:
                if ( mDocMode == dmTopLevel )
                    emit currentChanged( indexOf( td ) );
                break;
            case QEvent::WindowTitleChange:
                emit docTitleChanged( indexOf( td ), td->windowTitle().replace( "[*]", QString() ) );
            default:
                break;
            }
        }
    }

    // return default event filter
    return QWidget::eventFilter( object, event );
}

/*!
    \details Return the QTabbar associated with the workspace.
    \details Can be null.
*/
pTabBar* pExtendedWorkspace::tabBar() const
{ return NULL;}//mTabBar; }

/*!
    \details Return the pFilesListWidget widget used by the workspace.
*/
pFilesListWidget* pExtendedWorkspace::listWidget() const
{ return mFilesList; }

/*!
    \details Return the workspace mode
*/
pExtendedWorkspace::DocumentMode pExtendedWorkspace::docMode() const
{ return mDocMode; }

/*!
    \details Return the tabbar shape
*/
QTabBar::Shape pExtendedWorkspace::tabShape () const
{ return (QTabBar::Shape)0;}//mTabBar->shape(); }

/*!
    \details return the widgets list
*/
QWidgetList pExtendedWorkspace::documents() const
{ return mDocuments; }

/*!
    \details Return a widget or null if id is invalid
    \param id The id of the widget to get
*/
QWidget* pExtendedWorkspace::document( int id ) const
{ return mDocuments.value( id ); }

/*!
    \details Return the number of widget in this workspace
*/
int pExtendedWorkspace::count() const
{ return mDocuments.count(); }

/*!
    \details Return the id of the current widget
*/
int pExtendedWorkspace::currentIndex() const
{ return indexOf( currentDocument() ); }


/*!
    \details Return the current widget
*/
QWidget* pExtendedWorkspace::currentDocument() const
{
    switch ( mDocMode )
    {
        case dmSDI:
            return mStackedWidget->currentWidget();
            break;
        case dmMDI:
            return mMdiAreaWidget->currentSubWindow() ? mMdiAreaWidget->currentSubWindow()->widget() : 0;
            break;
        case dmTopLevel:
            foreach ( QWidget* w, mDocuments )
                if ( qApp->activeWindow() == w )
                    return w;
            if ( mDocuments.contains( mLastDocument ) )
                return mLastDocument;
            break;
    }
    return 0;
}

/*!
    \details Return a widget id, if widget is not managed by the workspace, -1 is returned.
    \param widget The widget to get id
*/
int pExtendedWorkspace::indexOf( QWidget* widget ) const
{ return mDocuments.indexOf( widget ); }

/*!
    \details Set the worksapce background pixmap
    \param pixmap The pixmap used for background
*/
void pExtendedWorkspace::setBackground( const QPixmap& pixmap )
{ mMdiAreaWidget->setBackground( QBrush( pixmap ) ); }

/*!
    \details Set the worksapce background pixmap
    \param fileName The pixmap fileName to load
*/
void pExtendedWorkspace::setBackground( const QString& fileName )
{ mMdiAreaWidget->setBackground( QBrush( QPixmap( fileName ) ) ); }

/*!
    \details Add a new widget to the workspace
    \param widget The widget to add
    \param title The windowTitle to set
    \param icon The windowIcon to set
*/
int pExtendedWorkspace::addDocument(QWidget* widget, const QString& title,  const QIcon& icon )
{ return insertDocument( count(), widget, title, icon ); }

/*!
    \details Insert a new widget to the workspace at the given index
    \param id The widget position to add to
    \param widget The widget to add
    \param title The windowTitle to set
    \param icon The windowIcon to set
*/
int pExtendedWorkspace::insertDocument( int id, QWidget* widget, const QString& /*title*/,  const QIcon& icon )
{
    // filter the document
    widget->installEventFilter( this );
    widget->setAttribute( Qt::WA_DeleteOnClose, true );

    // append to document list
    mDocuments.insert( id, widget );
    
    switch ( mDocMode )
    {
    case dmSDI:
        mStackedLayout->setCurrentWidget( mStackedWidget );
        mStackedWidget->addWidget( widget );
        //mStackedWidget->setCurrentWidget( widget );
        break;
    case dmMDI:
    {
        QMdiSubWindow* w = mMdiAreaWidget->addSubWindow( widget );
        w->installEventFilter( this );
        w->showNormal();
        if ( !widget->isVisible() )
            widget->show();
        break;
    }
    case dmTopLevel:
        widget->setParent( 0 );
        widget->showNormal();
        break;
    }   
    
    // emit tab inserted
    emit documentInserted( id, /*title*/widget->windowTitle ().replace("[*]", "" ), icon );

    // emit tab current changed
    emit currentChanged( id );
    
    return id;
}

/*!
    \details Remove a widget at index from the workspace, and return it.
    \details The widget is not deleted.
    \details if id is invalid, null is returned
    \param id The id of the widget to take
*/
QWidget* pExtendedWorkspace::takeDocument( int id )
{
    if ( QWidget* w = mDocuments.value( id ) )
    {
        emit documentAboutToBeRemoved( id );
        mDocuments.removeAt( id );
        w->removeEventFilter( this );
        
        if ( mDocMode == dmMDI )
            foreach ( QMdiSubWindow* sw, mMdiAreaWidget->subWindowList() )
                if ( sw == w->parent() )
                    sw->deleteLater();
        
        w->setParent( 0 );
        w->close();
        emit currentChanged( currentIndex() );
        return w;
    }
    return 0;
}

/*!
    \details remove a widget from workspace at the given index
    \param id The widget index, do nothing if index is invalid
*/
void pExtendedWorkspace::removeDocument( int id )
{ takeDocument( id ); }

/*!
    \details Swap 2 widgets position inside the workspace
    \param fromId The source widget index
    \param toId The target widget index
*/
void pExtendedWorkspace::moveDocument( int fromId, int toId )
{
    QWidget* w = mDocuments[fromId];
    mDocuments.insert( toId, mDocuments.takeAt( fromId ) );
    if ( mDocMode == dmSDI )
        mStackedWidget->insertWidget( toId, w );
}

/*!
    \details Close a widget ( ie: inform about to close, and remvoe it form workspace )
    \param id The widget id to remove
*/
void pExtendedWorkspace::closeDocument( int id )
{
    emit documentAboutToClose( id );
    removeDocument( id );
}

/*!
    \details Close a widget ( ie: inform about to close, and remvoe it form workspace )
    \param widget The widget to remove
*/
void pExtendedWorkspace::closeDocument( QWidget* widget )
{
    closeDocument( indexOf( widget ) );
}

/*!
    \details Close all documents
*/
bool pExtendedWorkspace::closeAllDocuments()
{
    for (int i = count()-1; i>=0; i--)
        closeDocument (i);
    return true;
}

/*!
    \details Close the current document
*/
void pExtendedWorkspace::closeCurrentDocument()
{
    closeDocument( currentIndex() );
}

/*!
    \details Change the workspace mode
    \param mode The new mode
*/
void pExtendedWorkspace::setDocMode( pExtendedWorkspace::DocumentMode mode )
{
    if ( mDocMode == mode )
        return;
    
    mDocMode = mode;

    if (mDocMode == dmSDI)
        mStackedLayout->setCurrentWidget( mStackedWidget );
    else if (mDocMode ==dmMDI )
        mStackedLayout->setCurrentWidget( mMdiAreaWidget );

    if (!count())
        return;
    
    int i = currentIndex(); //for avoid return from function because index not changed

    // add document to correct workspace
    foreach ( QWidget* td, mDocuments )
    {
        switch ( mDocMode )
        {
        case dmSDI:
            mStackedWidget->addWidget( td );
            //foreach (QAction* act, mMainWindow->actions ()) // not working !!! FIXME
            //  td->removeAction (act);
            break;
        case dmMDI:
        {
            QMdiSubWindow* w = mMdiAreaWidget->addSubWindow( td );
            w->installEventFilter( this );
            w->showNormal();
            if ( !td->isVisible() )
                td->show();
            //foreach (QAction* act, mMainWindow->actions ())
            //  td->removeAction (act);
            break;
        }
        case dmTopLevel:
            td->setParent( 0 );
            //td->addActions (mMainWindow->actions());
            td->showNormal();
            break;
        }   
    }
    
    //cleanup QMdiArea - remove it's empty QMdiSubWindows
    if ( mDocMode != dmMDI )
        foreach (QMdiSubWindow* sw, mMdiAreaWidget->subWindowList ())
            delete  sw; //if just remove - widget will not be deleted
    
    // restore current index
    setCurrentIndex( i );
    
    // emit tab mode changed
    emit docModeChanged( mDocMode );
}

void pExtendedWorkspace::setTabShape( QTabBar::Shape )
{
/*  if ( tabShape() == s )
        return;
    
    // get sizepolicy
    QSizePolicy sp = mTabBar->sizePolicy();
    
    // update view layout
    switch ( s )
    {
    case QTabBar::RoundedNorth:
    case QTabBar::TriangularNorth:
        mLayout->setDirection( QBoxLayout::TopToBottom );
        break;
    case QTabBar::RoundedSouth:
    case QTabBar::TriangularSouth:
        mLayout->setDirection( QBoxLayout::BottomToTop );
        break;
    default:
        Q_ASSERT (0);
    }
    
    // apply tab shape
    mTabBar->setShape( s );
    
    // update corners
    //updateCorners();
    
    // emit shape changed
    emit tabShapeChanged( s );*/
}

/*!
    \details Change the current indexwidget
    \param id The widget id to set current
*/
void pExtendedWorkspace::setCurrentIndex( int id )
{
    if ( currentIndex() == id )
        return;
    
    // get document
    QWidget* w = document( id );
    
    // update gui if needed
    if ( id != -1 )
    {
        switch ( mDocMode )
        {
            case dmSDI:
                mStackedWidget->setCurrentWidget( w );
            case dmMDI:
                mMdiAreaWidget->setActiveSubWindow( qobject_cast<QMdiSubWindow*>( w ? w->parent() : 0 ) );
                break;
            case dmTopLevel:
                if ( w && !w->isActiveWindow() )
                    w->activateWindow();
                break;
        }
    }
    
    // emit document change
    emit currentChanged( id );
}

/*!
    \details Set the current widget
    \param widget The widget to set current
*/
void pExtendedWorkspace::setCurrentDocument( QWidget* widget )
{ setCurrentIndex( indexOf( widget ) ); }

void pExtendedWorkspace::mdiArea_subWindowActivated( QMdiSubWindow* w )
{ emit currentChanged( w ? indexOf( w->widget() ) : -1 ); }

void pExtendedWorkspace::internal_currentChanged( int id )
{ mLastDocument = document( id ); }

/*!
    \details Set the next widget the current one, the process is wrapping ( ie, if at last activate first )
*/
void pExtendedWorkspace::activateNextDocument()
{
    int currIndex = currentIndex();
    if ( currIndex +1 == count() )
        setCurrentIndex( 0 );
    else
        setCurrentIndex( currIndex +1 );
}

/*!
    \details Set the previous widget the current one, the process is wrapping ( ie, if at first activate last )
*/
void pExtendedWorkspace::activatePreviousDocument()
{
    int currIndex = currentIndex();
    if ( currIndex -1 == -1 )
        setCurrentIndex( count() -1 );
    else
        setCurrentIndex( currIndex -1 );
}

/*!
    \details Change the workspace mode to SDI
*/
void pExtendedWorkspace::setSDI()
{
    setDocMode (dmSDI);
}

/*!
    \details Change the workspace mode to MDI
*/
void pExtendedWorkspace::setMDI()
{
    setDocMode (dmMDI);
}

/*!
    \details Change the workspace mode to TopLevel
*/
void pExtendedWorkspace::setTopLevel()
{
    setDocMode (dmTopLevel);
}

/*!
    \details Cascade the documents, switching to MDI mode if needed first.
*/
void pExtendedWorkspace::cascade()
{
    setDocMode( dmMDI );
    mMdiAreaWidget->cascadeSubWindows();
};

/*!
    \details Tile the documents, switching to MDI mode if needed first.
*/
void pExtendedWorkspace::tile()
{
    setDocMode( dmMDI );
    mMdiAreaWidget->tileSubWindows();
};

/*!
    \details Minimize the documents, switching to MDI mode if needed first.
*/
void pExtendedWorkspace::minimize()
{
    setDocMode( dmMDI );
    foreach ( QWidget* w, mDocuments )
        w->showMinimized();
};

/*!
    \details Restore the documents, switching to MDI mode if needed first.
*/
void pExtendedWorkspace::restore()
{
    setDocMode( dmMDI );
    foreach ( QWidget* w, mDocuments )
        w->showNormal();
};