kigfiledialog.cc 3.26 KB
Newer Older
Pino Toscano's avatar
Pino Toscano committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (C)  2005  Pino Toscano       <toscano.pino@tiscali.it>

// 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
Dirk Mueller's avatar
Dirk Mueller committed
15
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
Pino Toscano's avatar
Pino Toscano committed
16 17 18 19
// USA

#include "kigfiledialog.h"

David Narváez's avatar
David Narváez committed
20 21
#include <QVBoxLayout>
#include <QPushButton>
Pino Toscano's avatar
Pino Toscano committed
22 23 24 25 26
#include <qfile.h>

#include <klocale.h>
#include <kmessagebox.h>

David Narváez's avatar
David Narváez committed
27 28
#include <cassert>

Pino Toscano's avatar
Pino Toscano committed
29 30
KigFileDialog::KigFileDialog( const QString& startDir, const QString& filter,
              const QString& caption,  QWidget* parent )
David Narváez's avatar
David Narváez committed
31
  : QFileDialog( parent, caption, startDir, filter ),
Pino Toscano's avatar
Pino Toscano committed
32 33
    mow( 0L )
{
David Narváez's avatar
David Narváez committed
34 35
  setAcceptMode( QFileDialog::AcceptSave );
  setFileMode( QFileDialog::AnyFile );
Pino Toscano's avatar
Pino Toscano committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  moptcaption = i18n( "Options" );
}

void KigFileDialog::setOptionsWidget( QWidget* w )
{
  mow = w;
}

void KigFileDialog::accept()
{
  // i know this is an ugly hack, but i hadn't found other ways to get
  // the selected file name _before_ the dialog is accept()'ed or
  // reject()'ed... in every case, below we make sure to accept() or
  // reject()...
  setResult( QDialog::Accepted );

  QString sFile = selectedFile();
David Narváez's avatar
David Narváez committed
53

Pino Toscano's avatar
Pino Toscano committed
54 55 56
  if ( QFile::exists( sFile ) )
  {
    int ret = KMessageBox::warningContinueCancel( this,
57
                                         i18n( "The file \"%1\" already exists. Do you wish to overwrite it?" ,
58
                                           sFile ), i18n( "Overwrite File?" ), KStandardGuiItem::overwrite() );
Pino Toscano's avatar
Pino Toscano committed
59 60
    if ( ret != KMessageBox::Continue )
    {
David Narváez's avatar
David Narváez committed
61
      QFileDialog::reject();
Pino Toscano's avatar
Pino Toscano committed
62 63 64 65 66
      return;
    }
  }
  if ( mow )
  {
David Narváez's avatar
David Narváez committed
67
    QDialog* optdlg = new QDialog( this );
David Narváez's avatar
David Narváez committed
68 69 70 71 72 73 74 75 76 77
    QWidget* mainWidget = new QWidget( optdlg );
    QDialogButtonBox* buttonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Ok );
    QPushButton *okButton = buttonBox->button( QDialogButtonBox::Ok );
    QVBoxLayout* mainLayout = new QVBoxLayout;
    
    okButton->setDefault( true );
    okButton->setShortcut( Qt::CTRL | Qt::Key_Return );
    optdlg->setLayout( mainLayout );
    mainLayout->addWidget( mainWidget );
    mainLayout->addWidget( buttonBox );
David Narváez's avatar
David Narváez committed
78
    optdlg->setWindowTitle( moptcaption );
David Narváez's avatar
David Narváez committed
79 80 81 82
    
    connect( buttonBox, SIGNAL( accepted() ), optdlg, SLOT( accept() ) );
    connect( buttonBox, SIGNAL( rejected() ), this, SLOT( reject() ));

Pino Toscano's avatar
Pino Toscano committed
83
    mow->setParent( optdlg );
David Narváez's avatar
David Narváez committed
84
//PORTING: Verify that widget was added to mainLayout     optdlg->setMainWidget( mow );
David Narváez's avatar
David Narváez committed
85
    optdlg->exec() == QDialog::Accepted ? QFileDialog::accept() : QFileDialog::reject();
Pino Toscano's avatar
Pino Toscano committed
86 87
  }
  else
David Narváez's avatar
David Narváez committed
88
    QFileDialog::accept();
Pino Toscano's avatar
Pino Toscano committed
89 90 91 92 93 94 95 96 97
}

void KigFileDialog::setOptionCaption( const QString& caption )
{
  if ( caption.isEmpty() )
    return;

  moptcaption = caption;
}
David Narváez's avatar
David Narváez committed
98 99 100 101 102 103 104 105 106

QString KigFileDialog::selectedFile()
{
  QStringList files = selectedFiles();

  assert( files.size() == 1 );

  return files[0];
}
David Narváez's avatar
David Narváez committed
107