~ubuntu-branches/ubuntu/intrepid/gwenview/intrepid

« back to all changes in this revision

Viewing changes to src/gvimagesavedialog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christopher Martin
  • Date: 2005-04-06 11:33:06 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050406113306-7zovl7z0io5bacpd
Tags: 1.2.0-1
* New upstream release.
  + Fixes crashes when using "Back" to navigate. (Closes: #301811)
* Enable KIPI support.
* Add a doc-base file for the handbook.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// vim: set tabstop=4 shiftwidth=4 noexpandtab
 
2
/*
 
3
Gwenview - A simple image viewer for KDE
 
4
Copyright 2000-2004 Aur�lien G�teau
 
5
 
 
6
This program is free software; you can redistribute it and/or
 
7
modify it under the terms of the GNU General Public License
 
8
as published by the Free Software Foundation; either version 2
 
9
of the License, or (at your option) any later version.
 
10
 
 
11
This program is distributed in the hope that it will be useful,
 
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
GNU General Public License for more details.
 
15
 
 
16
You should have received a copy of the GNU General Public License
 
17
along with this program; if not, write to the Free Software
 
18
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
19
 
 
20
*/
 
21
// Qt includes
 
22
#include <qtimer.h>
 
23
 
 
24
// KDE includes
 
25
#include <kdebug.h>
 
26
#include <kdeversion.h>
 
27
#include <kfilefiltercombo.h>
 
28
#include <kimageio.h>
 
29
#include <klocale.h>
 
30
#include <kurlcombobox.h>
 
31
 
 
32
// Our includes
 
33
#include "gvimagesavedialog.moc"
 
34
 
 
35
 
 
36
static int findFormatInFilterList(const QStringList& filters, const QString& format) {
 
37
        int pos=0;
 
38
        for(QStringList::const_iterator it=filters.begin(); it!=filters.end(); ++it,++pos) {
 
39
                QStringList list=QStringList::split("|",*it);
 
40
                if ( list[1].startsWith(format) ) return pos;
 
41
        }
 
42
        return -1;
 
43
}
 
44
 
 
45
 
 
46
GVImageSaveDialog::GVImageSaveDialog(KURL& url, const QCString& imageFormat, QWidget* parent)
 
47
: KFileDialog(url.path(),QString::null,parent,"gvimagesavedialog",true)
 
48
, mURL(url)
 
49
, mImageFormat(imageFormat)
 
50
{
 
51
        setOperationMode(KFileDialog::Saving);
 
52
 
 
53
        // FIXME: Ugly code to define the filter combo label.
 
54
        KMimeType::List types;
 
55
        setFilterMimeType(i18n("Format:"),types,KMimeType::mimeType(""));
 
56
        
 
57
        QStringList filters;
 
58
 
 
59
        // Create our filter list
 
60
        QStringList mimeTypes=KImageIO::mimeTypes();
 
61
        for(QStringList::const_iterator it=mimeTypes.begin(); it!=mimeTypes.end(); ++it) {
 
62
                QString format=KImageIO::typeForMime(*it);
 
63
                
 
64
                // Create the pattern part of the filter string
 
65
                KMimeType::Ptr mt=KMimeType::mimeType(*it);
 
66
                QStringList patterns;
 
67
                for (QStringList::const_iterator patIt=mt->patterns().begin();patIt!=mt->patterns().end();++patIt) {
 
68
                        QString pattern=(*patIt).lower();
 
69
                        if (!patterns.contains(pattern)) patterns.append(pattern);
 
70
                }
 
71
                if (patterns.isEmpty()) {
 
72
                        patterns.append( QString("*.%1").arg(format.lower()) );
 
73
                }
 
74
                QString patternString=patterns.join(" ");
 
75
 
 
76
                // Create the filter string
 
77
                QString filter=QString("%1|%2 - %3 (%4)").arg(patternString).arg(format).arg(mt->comment()).arg(patternString);
 
78
                
 
79
                // Add it to our list
 
80
                filters.append(filter);
 
81
        }
 
82
        
 
83
        qHeapSort(filters);
 
84
        setFilter(filters.join("\n"));
 
85
        
 
86
        // Select the default image format
 
87
        int pos=findFormatInFilterList(filters,mImageFormat);
 
88
        if (pos==-1) {
 
89
                pos=findFormatInFilterList(filters,"PNG");
 
90
                mImageFormat="PNG";
 
91
        }
 
92
        
 
93
        filterWidget->setCurrentItem(pos);
 
94
 
 
95
        // Tweak the filter widget
 
96
        filterWidget->setEditable(false);
 
97
        
 
98
        connect(filterWidget,SIGNAL(activated(const QString&)),
 
99
                this,SLOT(updateImageFormat(const QString&)) );
 
100
        
 
101
        // Call slotFilterChanged() to get the list filtered by the filter we
 
102
        // selected. If we don't use a single shot, it leads to a crash :-/
 
103
        QTimer::singleShot(0,this,SLOT(slotFilterChanged()));
 
104
}
 
105
 
 
106
 
 
107
void GVImageSaveDialog::accept() {
 
108
        KFileDialog::accept();
 
109
        mURL=selectedURL();
 
110
}
 
111
 
 
112
 
 
113
void GVImageSaveDialog::updateImageFormat(const QString& text) {
 
114
        QStringList list=QStringList::split(" ",text);
 
115
        mImageFormat=list[0].local8Bit();
 
116
        
 
117
        QString name=locationEdit->currentText();
 
118
        QString suffix=KImageIO::suffix(mImageFormat);
 
119
        int dotPos=name.findRev('.');
 
120
        if (dotPos>-1) {
 
121
                name=name.left(dotPos);
 
122
        } 
 
123
        name.append('.').append(suffix);
 
124
        locationEdit->setCurrentText(name);
 
125
}
 
126