File: RecentFilesMenu.cpp

package info (click to toggle)
qtop 2.3.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,976 kB
  • sloc: cpp: 40,477; makefile: 7
file content (156 lines) | stat: -rw-r--r-- 5,279 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
/******************************************************************************
*
* Copyright (C) 2002 Hugo PEREIRA <mailto: hugo.pereira@free.fr>
*
* This 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 software 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, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************/

#include "RecentFilesMenu.h"

#include "BaseIconNames.h"
#include "FileRecordBaseProperties.h"
#include "IconEngine.h"
#include "QuestionDialog.h"
#include "XmlOptions.h"

#include <algorithm>

//_______________________________________________
RecentFilesMenu::RecentFilesMenu( QWidget *parent, FileList& files ):
    QMenu( parent ),
    Counter( "RecentFilesMenu" ),
    fileList_( &files )
{
    Debug::Throw( "RecentFilesMenu::RecentFilesMenu.\n" );

    setTitle( tr( "Open Recent" ) );
    connect( this, SIGNAL(triggered(QAction*)), SLOT(_open(QAction*)) );
    connect( this, SIGNAL(aboutToShow()), fileList_, SLOT(checkValidFiles()) );
    connect( this, SIGNAL(aboutToShow()), SLOT(_loadFiles()) );
    connect( fileList_, SIGNAL(validFilesChecked()), SLOT(_updateActions()) );

    // icons
    setIcon( IconEngine::get( IconNames::Open ) );

    actionGroup_ = new QActionGroup( this );
    actionGroup_->setExclusive( true );

    addAction( cleanAction_ = new QAction( IconEngine::get( IconNames::Delete ), tr( "Clean" ), this ) );
    connect( cleanAction_, SIGNAL(triggered()), SLOT(_clean()) );
    cleanAction_->setEnabled( false );
    addSeparator();

}

//______________________________________
bool RecentFilesMenu::openLastValidFile()
{
    Debug::Throw( "RecentFilesMenu::openLastValidFile.\n" );
    const FileRecord record( fileList_->lastValidFile() );
    if( record.file().isEmpty() ) return false;
    else {
        emit fileSelected( record );
        return true;
    }
}

//______________________________________
void RecentFilesMenu::setCurrentFile( const File& file )
{ if( !file.isEmpty() )  setCurrentFile( fileList_->add( file.expanded() ) ); }

//______________________________________
void RecentFilesMenu::_updateActions()
{

    Debug::Throw( "RecentFilesMenu::_updateActions.\n" );

    // set actions enability
    FileRecord::List records( fileList_->records() );
    for( auto&& iter = actions_.begin(); iter != actions_.end(); ++iter )
    {

        FileRecord::List::const_iterator found = std::find_if(
            records.begin(),
            records.end(),
            FileRecord::SameFileFTor( iter.value().file() ) );
        if( found == records.end() ) continue;
        iter.value().setValid( found->isValid() );
        iter.key()->setEnabled( found->isValid() );

    }

    cleanAction_->setEnabled( fileList_->cleanEnabled() );

}

//______________________________________
void RecentFilesMenu::_clean()
{
    if( !fileList_->check() && !QuestionDialog( this, tr( "Clear list ?" ) ).exec() ) return;
    else if( fileList_->check() && !QuestionDialog( this, tr( "Remove invalid or duplicated files from list ?" ) ).exec() ) return;
    fileList_->clean();
}

//_______________________________________________
void RecentFilesMenu::_open( QAction* action )
{

    Debug::Throw( "RecentFilesMenu::_Open.\n" );

    // find Action in map
    ActionMap::iterator iter( actions_.find( action ) );
    if( iter == actions_.end() ) return;
    emit fileSelected( iter.value() );

}

//_______________________________________________
void RecentFilesMenu::_loadFiles()
{
    Debug::Throw( "RecentFilesMenu::_loadFiles.\n" );

    // run thread to check file validity
    cleanAction_->setEnabled( fileList_->cleanEnabled() );

    // clear menu an actions map
    for( auto&& iter = actions_.begin(); iter != actions_.end(); ++iter )
    { delete iter.key(); }
    actions_.clear();

    // redo all actions
    FileRecord::List records( fileList_->records() );
    if( XmlOptions::get().get<bool>("SORT_FILES_BY_DATE") ) { std::sort( records.begin(), records.end(), FileRecord::FirstOpenFTor() ); }
    else { std::sort( records.begin(), records.end(), FileRecord::FileFTor() ); }

    // retrieve stored file record
    for( const auto& record:records )
    {

        QString label( record.file() );
        QAction* action = addAction( label );

        // add icon
        if( record.hasProperty( FileRecordProperties::Icon ) ) { action->setIcon( IconEngine::get( record.property( FileRecordProperties::Icon ) ) ); }

        // check action if match file
        action->setCheckable( true );
        action->setChecked( record.file() == currentFile().file() );
        actionGroup_->addAction( action );

        if( fileList_->check() ) action->setEnabled( !record.file().isEmpty() && record.isValid() );
        actions_.insert( action, record );
    }

}