~logan/ubuntu/wily/pinentry/0.9.2

« back to all changes in this revision

Viewing changes to qt4/pinentrydialog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Peter Eisentraut
  • Date: 2010-03-18 23:36:51 UTC
  • mfrom: (1.1.6 upstream) (4.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20100318233651-w7bs3r8rdyaa6oty
* New upstream release
* Changed source format to 3.0 (quilt)
* Updated standards version

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 
27
27
#include "qsecurelineedit.h"
28
28
 
29
 
#include <QVBoxLayout>
30
 
#include <QHBoxLayout>
 
29
#include <QProgressBar>
 
30
#include <QApplication>
 
31
#include <QStyle>
 
32
#include <QPainter>
31
33
#include <QPushButton>
 
34
#include <QDialogButtonBox>
32
35
#include <QKeyEvent>
33
36
#include <QLabel>
34
 
#include <QMessageBox>
 
37
#include <QPalette>
 
38
 
 
39
#ifdef Q_WS_WIN
 
40
#include <windows.h>
 
41
#endif
 
42
 
 
43
#ifdef Q_WS_WIN
 
44
void SetForegroundWindowEx( HWND hWnd )
 
45
{
 
46
   //Attach foreground window thread to our thread
 
47
   const DWORD ForeGroundID = GetWindowThreadProcessId(::GetForegroundWindow(),NULL);
 
48
   const DWORD CurrentID   = GetCurrentThreadId();
 
49
 
 
50
   AttachThreadInput ( ForeGroundID, CurrentID, TRUE );
 
51
   //Do our stuff here
 
52
   HWND hLastActivePopupWnd = GetLastActivePopup( hWnd );
 
53
   SetForegroundWindow( hLastActivePopupWnd );
 
54
 
 
55
   //Detach the attached thread
 
56
   AttachThreadInput ( ForeGroundID, CurrentID, FALSE );
 
57
}// End SetForegroundWindowEx
 
58
#endif
 
59
 
 
60
void raiseWindow( QWidget* w )
 
61
{
 
62
#ifdef Q_WS_WIN
 
63
    SetForegroundWindowEx( w->winId() );
 
64
#endif
 
65
    w->raise();
 
66
    w->activateWindow();
 
67
}
 
68
 
 
69
QPixmap icon( QStyle::StandardPixmap which )
 
70
{
 
71
    QPixmap pm = qApp->windowIcon().pixmap( 48, 48 );
 
72
   
 
73
    if ( which != QStyle::SP_CustomBase ) {
 
74
        const QIcon ic = qApp->style()->standardIcon( which );
 
75
        QPainter painter( &pm );
 
76
        const int emblemSize = 22;
 
77
        painter.drawPixmap( pm.width()-emblemSize, 0,
 
78
                            ic.pixmap( emblemSize, emblemSize ) );
 
79
    }
 
80
 
 
81
    return pm;
 
82
}
35
83
 
36
84
PinEntryDialog::PinEntryDialog( QWidget* parent, const char* name, bool modal,
37
85
                                bool enable_quality_bar )
38
86
  : QDialog( parent ), _grabbed( false )
39
87
{
 
88
  setWindowFlags( windowFlags() & ~Qt::WindowContextHelpButtonHint );
 
89
 
40
90
  if ( modal ) {
41
91
    setWindowModality( Qt::ApplicationModal );
42
92
  }
43
93
 
44
 
  QBoxLayout* top = new QVBoxLayout( this );
45
 
  top->setMargin( 6 );
46
 
  QBoxLayout* upperLayout = new QHBoxLayout();
47
 
  top->addLayout( upperLayout );
48
 
 
49
94
  _icon = new QLabel( this );
50
 
  _icon->setPixmap( QMessageBox::standardIcon( QMessageBox::Information ) );
51
 
  upperLayout->addWidget( _icon );
52
 
 
53
 
  QBoxLayout* labelLayout = new QVBoxLayout();
54
 
  upperLayout->addLayout( labelLayout );
 
95
  _icon->setPixmap( icon() );
55
96
 
56
97
  _error = new QLabel( this );
57
98
  _error->setWordWrap(true);
58
 
  labelLayout->addWidget( _error );
 
99
  QPalette pal;
 
100
  pal.setColor( QPalette::WindowText, Qt::red );
 
101
  _error->setPalette( pal );
 
102
  _error->hide();
59
103
 
60
104
  _desc = new QLabel( this );
61
105
  _desc->setWordWrap(true);
62
 
  labelLayout->addWidget( _desc );
63
 
 
64
 
  QGridLayout* grid = new QGridLayout;
65
 
  top->addLayout( grid );
 
106
  _desc->hide();
66
107
 
67
108
  _prompt = new QLabel( this );
68
 
  grid->addWidget( _prompt, 0, 0 );
 
109
  _prompt->hide();
 
110
 
69
111
  _edit = new QSecureLineEdit( this );
70
112
  _edit->setMaxLength( 256 );
71
 
  grid->addWidget( _edit, 0, 1 );
 
113
 
 
114
  _prompt->setBuddy( _edit );
72
115
 
73
116
  if (enable_quality_bar)
74
117
  {
75
118
    _quality_bar_label = new QLabel( this );
76
119
    _quality_bar_label->setAlignment( Qt::AlignRight | Qt::AlignVCenter );
77
 
    grid->addWidget ( _quality_bar_label, 1, 0 );
78
120
    _quality_bar = new QProgressBar( this );
79
121
    _quality_bar->setAlignment( Qt::AlignCenter );
80
 
    grid->addWidget( _quality_bar, 1, 1 );
81
122
    _have_quality_bar = true;
82
123
  }
83
124
  else
84
125
    _have_quality_bar = false;
85
126
 
86
 
  QBoxLayout* l = new QHBoxLayout();
87
 
  top->addLayout( l );
88
 
 
89
 
  _ok = new QPushButton( tr("OK"), this );
90
 
  _cancel = new QPushButton( tr("Cancel"), this );
91
 
 
92
 
  l->addWidget( _ok );
93
 
  l->addStretch();
94
 
  l->addWidget( _cancel );
 
127
  QDialogButtonBox* const buttons = new QDialogButtonBox( this );
 
128
  buttons->setStandardButtons( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
 
129
  _ok = buttons->button( QDialogButtonBox::Ok );
 
130
  _cancel = buttons->button( QDialogButtonBox::Cancel );
95
131
 
96
132
  _ok->setDefault(true);
97
133
 
98
 
  connect( _ok, SIGNAL( clicked() ),
99
 
           this, SIGNAL( accepted() ) );
100
 
  connect( _cancel, SIGNAL( clicked() ),
101
 
           this, SIGNAL( rejected() ) );
 
134
  if ( style()->styleHint( QStyle::SH_DialogButtonBox_ButtonsHaveIcons ) )
 
135
    {
 
136
      _ok->setIcon( style()->standardIcon( QStyle::SP_DialogOkButton ) );
 
137
      _cancel->setIcon( style()->standardIcon( QStyle::SP_DialogCancelButton ) );
 
138
    }
 
139
 
 
140
  connect( buttons, SIGNAL(accepted()), this, SLOT(accept()) );
 
141
  connect( buttons, SIGNAL(rejected()), this, SLOT(reject()) );
102
142
  connect( _edit, SIGNAL( textChanged(secqstring) ),
103
143
           this, SLOT( updateQuality(secqstring) ) );
104
 
  connect (this, SIGNAL (accepted ()),
105
 
           this, SLOT (accept ()));
106
 
  connect (this, SIGNAL (rejected ()),
107
 
           this, SLOT (reject ()));
108
144
 
109
145
  _edit->setFocus();
 
146
 
 
147
  QGridLayout* const grid = new QGridLayout( this );
 
148
  grid->addWidget( _icon, 0, 0, 5, 1, Qt::AlignTop|Qt::AlignLeft );
 
149
  grid->addWidget( _error, 1, 1, 1, 2 );
 
150
  grid->addWidget( _desc,  2, 1, 1, 2 );
 
151
  //grid->addItem( new QSpacerItem( 0, _edit->height() / 10, QSizePolicy::Minimum, QSizePolicy::Fixed ), 1, 1 );
 
152
  grid->addWidget( _prompt, 3, 1 );
 
153
  grid->addWidget( _edit, 3, 2 );
 
154
  if( enable_quality_bar )
 
155
  {
 
156
    grid->addWidget( _quality_bar_label, 4, 1 );
 
157
    grid->addWidget( _quality_bar, 4, 2 );
 
158
  }
 
159
  grid->addWidget( buttons, 5, 0, 1, 3 );
 
160
 
 
161
  grid->setSizeConstraint( QLayout::SetFixedSize );
110
162
}
111
163
 
112
164
void PinEntryDialog::hideEvent( QHideEvent* ev )
117
169
  QDialog::hideEvent( ev );
118
170
}
119
171
 
120
 
void PinEntryDialog::keyPressEvent( QKeyEvent* e )
 
172
void PinEntryDialog::showEvent( QShowEvent* event )
121
173
{
122
 
  if ( e->modifiers() == Qt::NoModifier && e->key() == Qt::Key_Escape ) {
123
 
    emit rejected();
124
 
    return;
125
 
  }
126
 
  QDialog::keyPressEvent( e );
 
174
    QDialog::showEvent( event );
 
175
    raiseWindow( this );
127
176
}
128
177
 
129
178
void PinEntryDialog::setDescription( const QString& txt )
130
179
{
 
180
  _desc->setVisible( !txt.isEmpty() );
131
181
  _desc->setText( txt );
132
 
  _icon->setPixmap( QMessageBox::standardIcon( QMessageBox::Information ) );
 
182
  _icon->setPixmap( icon() );
133
183
  setError( QString::null );
134
184
}
135
185
 
140
190
 
141
191
void PinEntryDialog::setError( const QString& txt )
142
192
{
143
 
  if( !txt.isNull() )_icon->setPixmap( QMessageBox::standardIcon( QMessageBox::Critical ) );
 
193
  if( !txt.isNull() )_icon->setPixmap( icon( QStyle::SP_MessageBoxCritical ) );
144
194
  _error->setText( txt );
 
195
  _error->setVisible( !txt.isEmpty() );
145
196
}
146
197
 
147
198
QString PinEntryDialog::error() const
162
213
void PinEntryDialog::setPrompt( const QString& txt )
163
214
{
164
215
  _prompt->setText( txt );
 
216
  _prompt->setVisible( !txt.isEmpty() );
165
217
}
166
218
 
167
219
QString PinEntryDialog::prompt() const
172
224
void PinEntryDialog::setOkText( const QString& txt )
173
225
{
174
226
  _ok->setText( txt );
 
227
  _ok->setVisible( !txt.isEmpty() );
175
228
}
176
229
 
177
230
void PinEntryDialog::setCancelText( const QString& txt )
178
231
{
179
232
  _cancel->setText( txt );
 
233
  _cancel->setVisible( !txt.isEmpty() );
180
234
}
181
235
 
182
236
void PinEntryDialog::setQualityBar( const QString& txt )