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
|
/**
* Copyright 2003 Braden MacDonald <[email protected]>
* Copyright 2003 Ravikiran Rajagopal <[email protected]>
*
* 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
*
*
* Please see the README
*
*/
/**
* @file UserInfo's Dialog for changing your face.
* @author Braden MacDonald
*/
#include <qstring.h>
#include <qlayout.h>
#include <qlabel.h>
#include <qpixmap.h>
#include <qimage.h>
#include <qpushbutton.h>
#include <qdir.h>
#include <qcheckbox.h>
#include <kdialogbase.h>
#include <klocale.h>
#include <kfiledialog.h>
#include <kiconview.h>
#include <kimagefilepreview.h>
#include <kimageio.h>
#include <kmessagebox.h>
#include <konq_operations.h>
#include <kurl.h>
#include "chfacedlg.h"
#include "settings.h" // KConfigXT
/**
* TODO: It would be nice if the widget were in a .ui
*/
ChFaceDlg::ChFaceDlg(const QString& picsdir, QWidget *parent, const char *name, bool modal)
: KDialogBase( parent, name, modal, i18n("Change your Face"), Ok|Cancel, Ok, true )
{
QWidget *page = new QWidget(this);
setMainWidget( page );
QVBoxLayout *top = new QVBoxLayout(page, 0, spacingHint());
QLabel *header = new QLabel( i18n("Select a new face:"), page );
top->addWidget( header );
m_FacesWidget = new KIconView( page );
m_FacesWidget->setSelectionMode( QIconView::Single );
m_FacesWidget->setItemsMovable( false );
m_FacesWidget->setMinimumSize( 400, 200 );
connect( m_FacesWidget, SIGNAL( selectionChanged( QIconViewItem * ) ), SLOT( slotFaceWidgetSelectionChanged( QIconViewItem * ) ) );
connect( m_FacesWidget, SIGNAL( doubleClicked( QIconViewItem *, const QPoint & ) ), SLOT( slotOk() ) );
top->addWidget( m_FacesWidget );
// Buttons to get more pics
QHBoxLayout * morePics = new QHBoxLayout( 0, 0, spacingHint() );
QPushButton *browseBtn = new QPushButton( i18n("Custom &Image..."), page );
connect( browseBtn, SIGNAL( clicked() ), SLOT( slotGetCustomImage() ) );
morePics->addWidget( browseBtn );
#if 0
QPushButton *acquireBtn = new QPushButton( i18n("&Acquire Image..."), page );
acquireBtn->setEnabled( false );
morePics->addWidget( acquireBtn );
#endif
morePics->addStretch();
top->addLayout( morePics );
// Filling the icon view
QDir facesDir( picsdir );
if ( facesDir.exists() )
{
QStringList picslist = facesDir.entryList( QDir::Files );
for ( QStringList::Iterator it = picslist.begin(); it != picslist.end(); ++it )
new QIconViewItem( m_FacesWidget, (*it).section(".",0,0), QPixmap( picsdir + *it ) );
}
facesDir.setPath( KCFGUserAccount::userFaceDir() );
if ( facesDir.exists() )
{
QStringList picslist = facesDir.entryList( QDir::Files );
for ( QStringList::Iterator it = picslist.begin(); it != picslist.end(); ++it )
new QIconViewItem( m_FacesWidget, "/"+(*it) == KCFGUserAccount::customFaceFile() ?
i18n("(Custom)") : (*it).section(".",0,0),
QPixmap( KCFGUserAccount::userFaceDir() + *it ) );
}
m_FacesWidget->setResizeMode( QIconView::Adjust );
//m_FacesWidget->setGridX( FACE_PIX_SIZE - 10 );
m_FacesWidget->arrangeItemsInGrid();
enableButtonOK( false );
//connect( this, SIGNAL( okClicked() ), SLOT( slotSaveCustomImage() ) );
resize( 420, 400 );
}
void ChFaceDlg::addCustomPixmap( QString imPath, bool saveCopy )
{
QImage pix( imPath );
// TODO: save pix to TMPDIR/userinfo-tmp,
// then scale and copy *that* to ~/.faces
if (pix.isNull())
{
KMessageBox::sorry( this, i18n("There was an error loading the image.") );
return;
}
if ( (pix.width() > KCFGUserAccount::faceSize())
|| (pix.height() > KCFGUserAccount::faceSize()) )
pix = pix.scale( KCFGUserAccount::faceSize(), KCFGUserAccount::faceSize(), QImage::ScaleMin );// Should be no bigger than certain size.
if ( saveCopy )
{
// If we should save a copy:
QDir userfaces( KCFGUserAccount::userFaceDir() );
if ( !userfaces.exists( ) )
userfaces.mkdir( userfaces.absPath() );
pix.save( userfaces.absPath() + "/.userinfo-tmp" , "PNG" );
KonqOperations::copy( this, KonqOperations::COPY, KURL::List( KURL( userfaces.absPath() + "/.userinfo-tmp" ) ), KURL( userfaces.absPath() + "/" + QFileInfo(imPath).fileName().section(".",0,0) ) );
#if 0
if ( !pix.save( userfaces.absPath() + "/" + imPath , "PNG" ) )
KMessageBox::sorry(this, i18n("There was an error saving the image:\n%1").arg( userfaces.absPath() ) );
#endif
}
QIconViewItem* newface = new QIconViewItem( m_FacesWidget, QFileInfo(imPath).fileName().section(".",0,0) , pix );
newface->setKey( KCFGUserAccount::customKey() );// Add custom items to end
m_FacesWidget->ensureItemVisible( newface );
m_FacesWidget->setCurrentItem( newface );
}
void ChFaceDlg::slotGetCustomImage( )
{
QCheckBox* checkWidget = new QCheckBox( i18n("&Save copy in custom faces folder for future use"), 0 );
KFileDialog *dlg = new KFileDialog( QDir::homeDirPath(), KImageIO::pattern( KImageIO::Reading ),
this, 0, true, checkWidget);
dlg->setOperationMode( KFileDialog::Opening );
dlg->setCaption( i18n("Choose Image") );
dlg->setMode( KFile::File | KFile::LocalOnly );
KImageFilePreview *ip = new KImageFilePreview( dlg );
dlg->setPreviewWidget( ip );
if (dlg->exec() == QDialog::Accepted)
addCustomPixmap( dlg->selectedFile(), checkWidget->isChecked() );
// Because we give it a parent we have to close it ourselves.
dlg->close(true);
}
#if 0
void ChFaceDlg::slotSaveCustomImage()
{
if ( m_FacesWidget->currentItem()->key() == USER_CUSTOM_KEY)
{
QDir userfaces( QDir::homeDirPath() + USER_FACES_DIR );
if ( !userfaces.exists( ) )
userfaces.mkdir( userfaces.absPath() );
if ( !m_FacesWidget->currentItem()->pixmap()->save( userfaces.absPath() + USER_CUSTOM_FILE , "PNG" ) )
KMessageBox::sorry(this, i18n("There was an error saving the image:\n%1").arg( userfaces.absPath() ) );
}
}
#endif
#include "chfacedlg.moc"
|