File: OpenWithDialog.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 (228 lines) | stat: -rw-r--r-- 7,417 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
/******************************************************************************
*
* 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 "OpenWithDialog.h"

#include "Command.h"
#include "Debug.h"
#include "BaseFileInfo.h"
#include "BaseFileInfoModel.h"
#include "BaseIconNames.h"
#include "ElidedLabel.h"
#include "IconEngine.h"
#include "InformationDialog.h"
#include "TreeView.h"
#include "XmlOptions.h"

#include <QDesktopServices>
#include <QHeaderView>
#include <QLabel>
#include <QLayout>
#include <QScrollBar>
#include <QUrl>

//____________________________________________________________________________
OpenWithDialog::OpenWithDialog( QWidget* parent ):
    CustomDialog( parent, OkButton|CancelButton|Separator )
{
    Debug::Throw( "OpenWithDialog::OpenWithDialog.\n" );
    okButton().setText( tr( "Open" ) );
}

//____________________________________________________________________________
bool OpenWithDialog::isCommandValid() const
{ return comboBox_->isItemValid(); }

//____________________________________________________________________________
bool OpenWithDialog::isCommandDefault() const
{ return comboBox_->currentIndex() == 0; }

//____________________________________________________________________________
File OpenWithDialog::command() const
{ return comboBox_->command(); }

//____________________________________________________________________________
void OpenWithDialog::realizeWidget()
{

    Q_ASSERT( !files_.isEmpty() );

    // try load Question icon
    QHBoxLayout *hLayout( new QHBoxLayout );
    hLayout->setSpacing(10);
    hLayout->setMargin(0);
    mainLayout().addLayout( hLayout );
    {
        // icon
        QLabel* label = new QLabel( this );
        QIcon icon = icon_;
        if( icon.isNull() ) icon = IconEngine::get( IconNames::DialogWarning );
        label->setPixmap( icon.pixmap( iconSize() ) );
        hLayout->addWidget( label, 0 );

    }

    {
        // vertical layout for question etc.
        QVBoxLayout* vLayout = new QVBoxLayout;
        vLayout->setMargin(0);
        hLayout->addLayout( vLayout, 1 );

        vLayout->addStretch();

        if( isLink_ )
        {

            Q_ASSERT( files_.size() == 1 );

            vLayout->addWidget( new QLabel( tr( "Open link:" ), this ) );
            ElidedLabel* fileLabel = new ElidedLabel( files_.front(), this );
            fileLabel->setElideMode( Qt::ElideMiddle );

            // change font
            QFont font( fileLabel->font() );
            font.setStyle( QFont::StyleItalic );
            fileLabel->setFont( font );

            vLayout->addWidget( fileLabel );

        } else if( files_.size() > 1 ) {

            vLayout->addWidget( new QLabel( tr( "Open %1 files ? " ).arg( files_.size() ), this ) );

        } else {

            vLayout->addWidget( new QLabel( tr( "Open file \"%1\" ? " ).arg( files_.front().localName() ), this ) );

        }

        vLayout->addStretch();
    }

    if( files_.size() > 1 )
    {
        BaseFileInfo::List fileInfoList;
        for( const auto& file:files_ )
        { fileInfoList.append( BaseFileInfo( file ) ); }

        // list of multiple files
        using FileInfoModel = BaseFileInfoModel<BaseFileInfo>;
        FileInfoModel* model = new FileInfoModel( this );
        model->setShowIcons( false );
        model->set( fileInfoList );
        model->sort( FileInfoModel::Filename, Qt::DescendingOrder );

        // file list
        TreeView* treeView = new TreeView;
        treeView->setSelectionMode( QAbstractItemView::NoSelection );
        mainLayout().addWidget( treeView );

        treeView->setModel( model );
        treeView->toggleShowHeader( false );
        treeView->setMask( 1<<FileInfoModel::Filename );
        treeView->header()->setSortIndicator( FileInfoModel::Filename, Qt::AscendingOrder );

        // resize list to accomodate longest item
        int maxWidth( 0 );
        for( const auto& fileInfo:fileInfoList )
        { maxWidth = qMax( maxWidth, treeView->fontMetrics().width( fileInfo.file() ) ); }

        treeView->verticalScrollBar()->adjustSize();
        treeView->setMinimumSize( QSize(
            maxWidth + treeView->verticalScrollBar()->width() + 10,
            treeView->fontMetrics().height() + 10 ) );

    }

    {
        // horizontal layout for combobox
        QHBoxLayout *hLayout( new QHBoxLayout );
        hLayout->setMargin(0);
        mainLayout().addLayout( hLayout );

        QLabel* label;
        hLayout->addWidget( label = new QLabel( tr( "Open With:" ), this ), 0);
        hLayout->addWidget( comboBox_ = new OpenWithComboBox( this ), 1 );
        label->setAlignment( Qt::AlignVCenter|Qt::AlignRight );
        label->setBuddy( comboBox_ );

    }

    // setup combobox
    if( !optionName_.isEmpty() && XmlOptions::get().isSpecialOption( optionName_ ) )
    {
        // retrieve applications from options
        const auto applications( XmlOptions::get().specialOptions( optionName_ ) );
        for( const auto& option:applications )
        { comboBox_->addItem( File( option.raw() ) ); }
    }

    // sytem default
    comboBox_->insertItem(0, tr( "System Default" ) );
    comboBox_->setCurrentIndex( 0 );

    // connection
    connect( &okButton(), SIGNAL(clicked()), SLOT(_open()) );

}

//____________________________________________________________________________
void OpenWithDialog::_open()
{
    Debug::Throw( "OpenWithDialog::_open.\n" );

    // update options
    if( !optionName_.isEmpty() && XmlOptions::get().isSpecialOption( optionName_ ) )
    {
        for( const auto& command:comboBox_->newItems() )
        { XmlOptions::get().add( optionName_, Option( command, Option::Flag::Recordable|Option::Flag::Current ) ); }
    }

    // open
    if( autoOpen_ )
    {

        if( comboBox_->currentIndex() == 0 )
        {

            // open files using default application
            for( const auto& file:files_ )
            {
                if( isLink_ ) QDesktopServices::openUrl( QUrl::fromEncoded( file.get().toLatin1() ) );
                else QDesktopServices::openUrl( QUrl::fromEncoded( QString( "file://%1" ).arg( file ).toLatin1() ) );
            }

        } else {

            // retrieve application from combobox and add as options
            const QString command = comboBox_->command();
            if( command.isEmpty() || !comboBox_->isItemValid() )
            {
                InformationDialog( this, tr( "No command specified to open the selected files. <Open> canceled." ) ).exec();
                return;
            }

            // execute
            for( const auto& file:files_ ) { ( Command( command ) << file ).run(); }

        }

    }

}