26 #define YUILogComponent "qt-ui"
27 #include <yui/YUILog.h>
30 #include <qdatetimeedit.h>
34 #include "YQTimezoneSelector.h"
35 #include "YQWidgetCaption.h"
36 #include <yui/YEvent.h>
37 #include <QVBoxLayout>
39 #include <QMouseEvent>
43 #include "icons/zoom-in.xpm"
70 bool operator<(
const Location& l2)
const;
75 QList<Location> locations;
77 Location findBest(
const QPoint &pos )
const;
83 QPoint pixPosition(
const Location &pos )
const;
85 QPoint pixToWindow(
const QPoint &pos )
const;
91 convert_pos (
const QString &pos,
int digits)
93 if (pos.length() < 4 || digits > 9)
return 0.0;
95 QString whole = pos.left( digits + 1 );
96 QString fraction = pos.mid( digits + 1 );
98 float t1 = whole.toFloat();
99 float t2 = fraction.toFloat();
102 return t1 + t2/pow (10.0, fraction.length() );
104 return t1 - t2/pow (10.0, fraction.length());
107 bool YQTimezoneSelectorPrivate::Location::operator<(
const Location& l1 )
const
109 return l1.latitude < latitude;
113 : QFrame( (QWidget *) parent->widgetRep() )
114 , YTimezoneSelector( parent, pixmap, timezones )
118 setWidgetRep(
this );
119 setMouseTracking(
true);
120 d->_pix.load( fromUTF8( pixmap ) );
122 setStretchable( YD_HORIZ,
true );
123 setStretchable( YD_VERT,
true );
126 FILE *tzfile = fopen (
"/usr/share/zoneinfo/zone.tab",
"r");
127 while (fgets (buf,
sizeof(buf), tzfile))
129 if (*buf ==
'#')
continue;
132 QStringList arr = sbuf.trimmed().split(
'\t' );
135 while ( split_index < arr[1].length() && arr[1][split_index] !=
'-' && arr[1][split_index] !=
'+' )
139 loc.country = arr[0];
141 std::map<std::string, std::string>::const_iterator tooltip = timezones.find( loc.zone.toStdString() );
142 if (tooltip == timezones.end() )
145 loc.tip = fromUTF8( tooltip->second );
146 if ( arr.size() > 3 )
147 loc.comment = arr[3];
148 loc.latitude = convert_pos ( arr[1].left( split_index ), 2);
149 loc.longitude = convert_pos ( arr[1].mid( split_index ), 3);
151 loc.pix_pos = d->pixPosition( loc );
153 d->locations.push_back( loc );
158 qSort( d->locations.begin(), d->locations.end() );
160 d->blink =
new QTimer(
this );
161 d->blink->setInterval( 200 );
162 connect( d->blink, SIGNAL( timeout() ), SLOT( slotBlink() ) );
188 resize( newWidth, newHeight );
191 QPoint YQTimezoneSelectorPrivate::pixPosition(
const Location &pos )
const
193 return QPoint( (
int) ( _pix.width() / 2 + _pix.width() / 2 * pos.longitude / 180 ),
194 (
int) ( _pix.height() / 2 - _pix.height() / 2 * pos.latitude / 90 ) ) ;
197 void YQTimezoneSelector::mousePressEvent ( QMouseEvent * event )
199 if ( event->button() == Qt::RightButton )
202 d->cachePix = QPixmap();
204 else if ( event->button() == Qt::LeftButton )
206 d->_best = d->findBest( event->pos() );
208 if ( d->_zoom.isNull() )
210 QPoint click =
event->pos();
212 d->_zoom.rx() = (int) (
double( click.x() ) * d->_pix.width() / width() );
213 d->_zoom.ry() = (int) (
double( click.y() ) * d->_pix.height() / height() );
216 d->cachePix = QPixmap();
228 void YQTimezoneSelector::paintEvent( QPaintEvent *event )
230 QFrame::paintEvent( event );
233 if ( d->cachePix.width() != width() || d->cachePix.height() != height() )
234 d->cachePix = QPixmap();
236 if ( d->_zoom.isNull() )
238 if ( d->cachePix.isNull() )
240 QImage t = d->_pix.scaled( width(), height(), Qt::KeepAspectRatio );
241 d->cachePix = QPixmap::fromImage( t );
243 p.drawPixmap( ( width() - d->cachePix.width() ) / 2, ( height() - d->cachePix.height() ) / 2, d->cachePix );
245 setCursor( QCursor( QPixmap( zoom_in ) ) );
247 int left = qMin( qMax( d->_zoom.x() - width() / 2, 0 ), d->_pix.width() - width() );
248 int top = qMin( qMax( d->_zoom.y() - height() / 2, 0 ), d->_pix.height() - height() );
250 if ( d->cachePix.isNull() )
251 d->cachePix = QPixmap::fromImage( d->_pix.copy( QRect( QPoint( left, top ), size() ) ) );
253 p.drawPixmap( 0, 0, d->cachePix );
255 setCursor( Qt::CrossCursor );
258 p.setBrush( QColor(
"#D8DF57" ) );
259 p.setPen( QColor(
"#B9DFD6" ) );
260 for ( QList<YQTimezoneSelectorPrivate::Location>::const_iterator it = d->locations.begin(); it != d->locations.end(); ++it )
262 if ( !d->highlight || ( *it ).zone != d->_best.zone )
264 if ( d->_zoom.isNull() )
265 p.drawEllipse( QRect( d->pixToWindow( ( *it ).pix_pos ) - QPoint( 1,1 ), QSize( 3, 3 ) ) );
267 p.drawEllipse( QRect( d->pixToWindow( ( *it ).pix_pos ) - QPoint( 2,2 ), QSize( 5, 5 ) ) );
270 if ( d->highlight > 0 )
273 static const QColor blinks[] = { QColor(
"#00ff00" ), QColor(
"#22dd00" ), QColor(
"#44bb00" ),
274 QColor(
"#669900" ), QColor(
"#887700" ), QColor(
"#aa5500" ),
275 QColor(
"#887700" ), QColor(
"#669900" ), QColor(
"#44bb00" ),
276 QColor(
"#22dd00" ) };
277 int index = d->highlight - 1;
278 p.setPen( blinks[ index ] );
279 p.setBrush( blinks[ index ] );
281 p.drawEllipse( QRect( d->pixToWindow( d->_best.pix_pos ) - QPoint( 2,2 ), QSize( 5, 5 ) ) );
286 QFontMetrics fm( f );
288 QPoint off = d->pixToWindow( d->_best.pix_pos ) + QPoint( 11, 4 );
289 int tw = fm.width( d->_best.tip );
290 if ( tw + off.x() > width() )
291 off.rx() = d->pixToWindow( d->_best.pix_pos ).x() - tw - 10;
293 p.setPen( Qt::black );
294 p.drawText( off, d->_best.tip );
296 p.setPen( Qt::white );
297 p.drawText( off - QPoint( 1, 1 ), d->_best.tip );
304 double min_dist = 2000;
306 for ( QList<Location>::const_iterator it = locations.begin(); it != locations.end(); ++it )
308 double dist = QPoint( pixToWindow( ( *it ).pix_pos ) - pos ).manhattanLength ();
309 if ( dist < min_dist )
318 bool YQTimezoneSelector::event(QEvent *event)
320 if (event->type() == QEvent::ToolTip)
322 QHelpEvent *helpEvent =
static_cast<QHelpEvent *
>(event);
325 QToolTip::showText(helpEvent->globalPos(), best.tip );
327 return QWidget::event(event);
333 return d->_best.zone.toStdString();
336 QPoint YQTimezoneSelectorPrivate::pixToWindow(
const QPoint &pos )
const
338 if ( _zoom.isNull() )
340 return QPoint( (
int) (
double( pos.x() ) * cachePix.width() / _pix.width() ) + ( parent->width() - cachePix.width() ) / 2,
341 (
int) ( double( pos.y() ) * cachePix.height() / _pix.height() ) + ( parent->height() - cachePix.height() ) /2 );
343 int left = qMin( qMax( _zoom.x() - parent->width() / 2, 0 ), _pix.width() - parent->width() );
344 int top = qMin( qMax( _zoom.y() - parent->height() / 2, 0 ), _pix.height() - parent->height() );
346 return QPoint( pos.x() - left, pos.y() - top );
351 QString zone = fromUTF8( _zone );
353 if ( d->_best.zone == zone )
358 for ( QList<YQTimezoneSelectorPrivate::Location>::const_iterator it = d->locations.begin(); it != d->locations.end(); ++it )
360 if ( ( *it ).zone == zone )
365 d->_zoom = d->_best.pix_pos;
369 d->cachePix = QPixmap();
376 void YQTimezoneSelector::slotBlink()
378 if ( d->_best.zone.isNull() )
384 if ( d->highlight++ > 9 )
387 QPoint current = d->pixToWindow( d->_best.pix_pos );
388 update( QRect( current - QPoint( 3, 3 ), QSize( 7, 7 ) ) );
391 #include "YQTimezoneSelector.moc"
virtual int preferredHeight()
virtual void setSize(int newWidth, int newHeight)
YQTimezoneSelector(YWidget *parent, const std::string &pixmap, const std::map< std::string, std::string > &timezones)
void sendEvent(YEvent *event)
virtual ~YQTimezoneSelector()
virtual int preferredWidth()
virtual std::string currentZone() const
virtual void setCurrentZone(const std::string &zone, bool zoom)