1 | /** |
2 | * Copyright 2003 Braden MacDonald <bradenm_k@shaw.ca> |
3 | * Copyright 2003 Ravikiran Rajagopal <ravi@ee.eng.ohio-state.edu> |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License |
16 | * along with this program; if not, write to the Free Software |
17 | * |
18 | * |
19 | * Please see the README |
20 | * |
21 | */ |
22 | |
23 | /** |
24 | * @file UserInfo's Dialog for changing your face. |
25 | * @author Braden MacDonald |
26 | */ |
27 | |
28 | #include "chfacedlg.h" |
29 | |
30 | #include <QLayout> |
31 | #include <QLabel> |
32 | #include <QPixmap> |
33 | #include <QImage> |
34 | #include <QPushButton> |
35 | #include <QtCore/QDir> |
36 | #include <QCheckBox> |
37 | |
38 | #include <klocale.h> |
39 | #include <kfiledialog.h> |
40 | #include <kimagefilepreview.h> |
41 | #include <kimageio.h> |
42 | #include <kmessagebox.h> |
43 | #include <konq_operations.h> |
44 | #include <kurl.h> |
45 | |
46 | #include "settings.h" // KConfigXT |
47 | |
48 | |
49 | |
50 | /** |
51 | * TODO: It would be nice if the widget were in a .ui |
52 | */ |
53 | ChFaceDlg::ChFaceDlg(const QString& picsdir, QWidget *parent) |
54 | : KDialog( parent ) |
55 | { |
56 | setCaption( i18nc("@title:window" , "Change your Face" ) ); |
57 | setButtons( Ok|Cancel|User1|User2 ); |
58 | setDefaultButton( Ok ); |
59 | |
60 | setButtonText( User1, i18n("Custom Image..." ) ); |
61 | setButtonText( User2, i18n("Remove Image" ) ); |
62 | |
63 | QWidget *faceDlg = new QWidget; |
64 | ui.setupUi(faceDlg); |
65 | |
66 | setMainWidget(faceDlg); |
67 | |
68 | connect( ui.m_FacesWidget, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), SLOT(slotFaceWidgetSelectionChanged(QListWidgetItem*)) ); |
69 | |
70 | connect( ui.m_FacesWidget, SIGNAL(doubleClicked(QModelIndex)), SLOT(accept()) ); |
71 | connect( this, SIGNAL(okClicked()), this, SLOT(accept())); |
72 | |
73 | connect( this, SIGNAL(user1Clicked()), this, SLOT(slotGetCustomImage()) ); |
74 | |
75 | connect( this, SIGNAL(user2Clicked()), this, SLOT(slotRemoveImage()) ); |
76 | |
77 | #if 0 |
78 | QPushButton *acquireBtn = new QPushButton( i18n("&Acquire Image..." ), page ); |
79 | acquireBtn->setEnabled( false ); |
80 | morePics->addWidget( acquireBtn ); |
81 | #endif |
82 | |
83 | // Filling the icon view |
84 | QDir facesDir( picsdir ); |
85 | if ( facesDir.exists() ) |
86 | { |
87 | const QStringList picslist = facesDir.entryList( QDir::Files ); |
88 | for ( QStringList::const_iterator it = picslist.constBegin(); it != picslist.constEnd(); ++it ) |
89 | new QListWidgetItem( QIcon( picsdir + *it ), (*it).section('.',0,0), ui.m_FacesWidget ); |
90 | } |
91 | facesDir.setPath( KCFGUserAccount::userFaceDir() ); |
92 | if ( facesDir.exists() ) |
93 | { |
94 | const QStringList picslist = facesDir.entryList( QDir::Files ); |
95 | for ( QStringList::const_iterator it = picslist.constBegin(); it != picslist.constEnd(); ++it ) |
96 | new QListWidgetItem( QIcon( KCFGUserAccount::userFaceDir() + *it ), |
97 | QString('/'+(*it)) == KCFGUserAccount::customFaceFile() ? |
98 | i18n("(Custom)" ) : (*it).section('.',0,0), |
99 | ui.m_FacesWidget ); |
100 | } |
101 | |
102 | |
103 | enableButtonOk( false ); // since no item is pre-selected, we must only enable the Ok button once a selection is done! |
104 | //connect( this, SIGNAL(okClicked()), SLOT(slotSaveCustomImage()) ); |
105 | |
106 | resize( 420, 400 ); |
107 | } |
108 | |
109 | void ChFaceDlg::addCustomPixmap( const QString &imPath, bool saveCopy ) |
110 | { |
111 | QImage pix( imPath ); |
112 | // TODO: save pix to TMPDIR/userinfo-tmp, |
113 | // then scale and copy *that* to ~/.faces |
114 | |
115 | if (pix.isNull()) |
116 | { |
117 | KMessageBox::sorry( this, i18n("There was an error loading the image." ) ); |
118 | return; |
119 | } |
120 | if ( (pix.width() > KCFGUserAccount::faceSize()) |
121 | || (pix.height() > KCFGUserAccount::faceSize()) ) |
122 | pix = pix.scaled( KCFGUserAccount::faceSize(), KCFGUserAccount::faceSize(), Qt::KeepAspectRatio );// Should be no bigger than certain size. |
123 | |
124 | if ( saveCopy ) |
125 | { |
126 | // If we should save a copy: |
127 | QDir userfaces( KCFGUserAccount::userFaceDir() ); |
128 | if ( !userfaces.exists( ) ) |
129 | userfaces.mkdir( userfaces.absolutePath() ); |
130 | |
131 | pix.save( userfaces.absolutePath() + "/.userinfo-tmp" , "PNG" ); |
132 | KonqOperations::copy( this, KonqOperations::COPY, KUrl::List( KUrl( userfaces.absolutePath() + "/.userinfo-tmp" ) ), KUrl( userfaces.absolutePath() + '/' + QFileInfo(imPath).fileName().section('.',0,0) ) ); |
133 | #if 0 |
134 | if ( !pix.save( userfaces.absolutePath() + '/' + imPath , "PNG" ) ) |
135 | KMessageBox::sorry(this, i18n("There was an error saving the image:\n%1" , userfaces.absolutePath() ) ); |
136 | #endif |
137 | } |
138 | |
139 | QListWidgetItem* newface = new QListWidgetItem( QIcon(QPixmap::fromImage(pix)), QFileInfo(imPath).fileName().section('.',0,0), ui.m_FacesWidget ); |
140 | ui.m_FacesWidget->scrollToItem( newface ); |
141 | ui.m_FacesWidget->setCurrentItem( newface ); |
142 | } |
143 | |
144 | void ChFaceDlg::slotGetCustomImage( ) |
145 | { |
146 | QCheckBox* checkWidget = new QCheckBox( i18n("&Save copy in custom faces folder for future use" ), 0 ); |
147 | |
148 | KFileDialog dlg( QDir::homePath(), KImageIO::pattern( KImageIO::Reading ), |
149 | this, checkWidget); |
150 | |
151 | dlg.setOperationMode( KFileDialog::Opening ); |
152 | dlg.setCaption( i18nc("@title:window" , "Choose Image" ) ); |
153 | dlg.setMode( KFile::File | KFile::LocalOnly ); |
154 | |
155 | KImageFilePreview *ip = new KImageFilePreview( &dlg ); |
156 | dlg.setPreviewWidget( ip ); |
157 | if (dlg.exec() == QDialog::Accepted) |
158 | addCustomPixmap( dlg.selectedFile(), checkWidget->isChecked() ); |
159 | } |
160 | |
161 | void ChFaceDlg::slotRemoveImage() |
162 | { |
163 | ui.m_FacesWidget->clearSelection(); |
164 | accept(); |
165 | } |
166 | |
167 | #if 0 |
168 | void ChFaceDlg::slotSaveCustomImage() |
169 | { |
170 | if ( m_FacesWidget->currentItem()->key() == USER_CUSTOM_KEY) |
171 | { |
172 | QDir userfaces( QDir::homePath() + USER_FACES_DIR ); |
173 | if ( !userfaces.exists( ) ) |
174 | userfaces.mkdir( userfaces.absolutePath() ); |
175 | |
176 | if ( !m_FacesWidget->currentItem()->pixmap()->save( userfaces.absolutePath() + USER_CUSTOM_FILE , "PNG" ) ) |
177 | KMessageBox::sorry(this, i18n("There was an error saving the image:\n%1" , userfaces.absolutePath() ) ); |
178 | } |
179 | } |
180 | #endif |
181 | |
182 | #include "chfacedlg.moc" |
183 | |