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>
71 bool operator<(
const Location& l2)
const;
76 QList<Location> locations;
78 Location findBest(
const QPoint &pos )
const;
84 QPoint pixPosition(
const Location &pos )
const;
86 QPoint pixToWindow(
const QPoint &pos )
const;
93 convert_pos (
const QString &pos,
int digits)
95 if (pos.length() < 4 || digits > 9)
return 0.0;
97 QString whole = pos.left( digits + 1 );
98 QString fraction = pos.mid( digits + 1 );
100 float t1 = whole.toFloat();
101 float t2 = fraction.toFloat();
104 return t1 + t2/pow (10.0, fraction.length() );
106 return t1 - t2/pow (10.0, fraction.length());
110 bool YQTimezoneSelectorPrivate::Location::operator<(
const Location& l1 )
const
112 return l1.latitude < latitude;
117 const string & pixmap,
118 const std::map<string,string> & timezones )
119 : QFrame( (QWidget *) parent->widgetRep() )
120 , YTimezoneSelector( parent, pixmap, timezones )
124 setWidgetRep(
this );
125 setMouseTracking(
true);
126 d->_pix.load( fromUTF8( pixmap ) );
128 setStretchable( YD_HORIZ,
true );
129 setStretchable( YD_VERT,
true );
132 FILE *tzfile = fopen (
"/usr/share/zoneinfo/zone.tab",
"r");
133 while (fgets (buf,
sizeof(buf), tzfile))
135 if (*buf ==
'#')
continue;
138 QStringList arr = sbuf.trimmed().split(
'\t' );
141 while ( split_index < arr[1].length() && arr[1][split_index] !=
'-' && arr[1][split_index] !=
'+' )
145 loc.country = arr[0];
147 std::map<string, string>::const_iterator tooltip = timezones.find( loc.zone.toStdString() );
148 if (tooltip == timezones.end() )
151 loc.tip = fromUTF8( tooltip->second );
152 if ( arr.size() > 3 )
153 loc.comment = arr[3];
154 loc.latitude = convert_pos ( arr[1].left( split_index ), 2);
155 loc.longitude = convert_pos ( arr[1].mid( split_index ), 3);
157 loc.pix_pos = d->pixPosition( loc );
159 d->locations.push_back( loc );
164 std::sort( d->locations.begin(), d->locations.end() );
166 d->blink =
new QTimer(
this );
167 d->blink->setInterval( 200 );
168 connect( d->blink, &pclass(d->blink)::timeout,
169 this, &pclass(
this)::slotBlink );
195 resize( newWidth, newHeight );
199 QPoint YQTimezoneSelectorPrivate::pixPosition(
const Location &pos )
const
201 return QPoint( (
int) ( _pix.width() / 2 + _pix.width() / 2 * pos.longitude / 180 ),
202 (
int) ( _pix.height() / 2 - _pix.height() / 2 * pos.latitude / 90 ) ) ;
206 void YQTimezoneSelector::mousePressEvent ( QMouseEvent * event )
208 if ( event->button() == Qt::RightButton )
211 d->cachePix = QPixmap();
213 else if ( event->button() == Qt::LeftButton )
215 d->_best = d->findBest( event->pos() );
217 if ( d->_zoom.isNull() )
219 QPoint click =
event->pos();
221 d->_zoom.rx() = (int) (
double( click.x() ) * d->_pix.width() / width() );
222 d->_zoom.ry() = (int) (
double( click.y() ) * d->_pix.height() / height() );
225 d->cachePix = QPixmap();
238 void YQTimezoneSelector::paintEvent( QPaintEvent *event )
240 QFrame::paintEvent( event );
243 if ( d->cachePix.width() != width() || d->cachePix.height() != height() )
244 d->cachePix = QPixmap();
246 if ( d->_zoom.isNull() )
248 if ( d->cachePix.isNull() )
250 QImage t = d->_pix.scaled( width(), height(), Qt::KeepAspectRatio );
251 d->cachePix = QPixmap::fromImage( t );
253 p.drawPixmap( ( width() - d->cachePix.width() ) / 2, ( height() - d->cachePix.height() ) / 2, d->cachePix );
255 Q_INIT_RESOURCE( qt_icons );
256 QIcon icon = QIcon::fromTheme(
"zoom-in", QIcon(
":/zoom-in" ) );
258 if ( !icon.isNull() )
259 setCursor( QCursor( icon.pixmap( QSize( 16, 16 ) ) ) );
263 int left = qMin( qMax( d->_zoom.x() - width() / 2, 0 ), d->_pix.width() - width() );
264 int top = qMin( qMax( d->_zoom.y() - height() / 2, 0 ), d->_pix.height() - height() );
266 if ( d->cachePix.isNull() )
267 d->cachePix = QPixmap::fromImage( d->_pix.copy( QRect( QPoint( left, top ), size() ) ) );
269 p.drawPixmap( 0, 0, d->cachePix );
271 setCursor( Qt::CrossCursor );
274 p.setBrush( QColor(
"#D8DF57" ) );
275 p.setPen( QColor(
"#B9DFD6" ) );
277 for ( QList<YQTimezoneSelectorPrivate::Location>::const_iterator it = d->locations.begin(); it != d->locations.end(); ++it )
279 if ( !d->highlight || ( *it ).zone != d->_best.zone )
281 if ( d->_zoom.isNull() )
282 p.drawEllipse( QRect( d->pixToWindow( ( *it ).pix_pos ) - QPoint( 1,1 ), QSize( 3, 3 ) ) );
284 p.drawEllipse( QRect( d->pixToWindow( ( *it ).pix_pos ) - QPoint( 2,2 ), QSize( 5, 5 ) ) );
287 if ( d->highlight > 0 )
289 static const QColor blinks[] = { QColor(
"#00ff00" ), QColor(
"#22dd00" ), QColor(
"#44bb00" ),
290 QColor(
"#669900" ), QColor(
"#887700" ), QColor(
"#aa5500" ),
291 QColor(
"#887700" ), QColor(
"#669900" ), QColor(
"#44bb00" ),
292 QColor(
"#22dd00" ) };
293 int index = d->highlight - 1;
294 p.setPen( blinks[ index ] );
295 p.setBrush( blinks[ index ] );
297 p.drawEllipse( QRect( d->pixToWindow( d->_best.pix_pos ) - QPoint( 2,2 ), QSize( 5, 5 ) ) );
302 QFontMetrics fm( f );
304 QPoint off = d->pixToWindow( d->_best.pix_pos ) + QPoint( 11, 4 );
305 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
306 int tw = fm.horizontalAdvance( d->_best.tip );
309 int tw = fm.width( d->_best.tip );
311 if ( tw + off.x() > width() )
312 off.rx() = d->pixToWindow( d->_best.pix_pos ).x() - tw - 10;
314 p.setPen( Qt::black );
315 p.drawText( off, d->_best.tip );
317 p.setPen( Qt::white );
318 p.drawText( off - QPoint( 1, 1 ), d->_best.tip );
326 double min_dist = 2000;
328 for ( QList<Location>::const_iterator it = locations.begin(); it != locations.end(); ++it )
330 double dist = QPoint( pixToWindow( ( *it ).pix_pos ) - pos ).manhattanLength ();
331 if ( dist < min_dist )
341 bool YQTimezoneSelector::event(QEvent *event)
343 if (event->type() == QEvent::ToolTip)
345 QHelpEvent *helpEvent =
static_cast<QHelpEvent *
>(event);
348 QToolTip::showText(helpEvent->globalPos(), best.tip );
350 return QWidget::event(event);
356 return d->_best.zone.toStdString();
360 QPoint YQTimezoneSelectorPrivate::pixToWindow(
const QPoint &pos )
const
362 if ( _zoom.isNull() )
364 return QPoint( (
int) (
double( pos.x() ) * cachePix.width() / _pix.width() ) + ( parent->width() - cachePix.width() ) / 2,
365 (
int) (
double( pos.y() ) * cachePix.height() / _pix.height() ) + ( parent->height() - cachePix.height() ) /2 );
367 int left = qMin( qMax( _zoom.x() - parent->width() / 2, 0 ), _pix.width() - parent->width() );
368 int top = qMin( qMax( _zoom.y() - parent->height() / 2, 0 ), _pix.height() - parent->height() );
370 return QPoint( pos.x() - left, pos.y() - top );
376 QString zone = fromUTF8( _zone );
378 if ( d->_best.zone == zone )
383 for ( QList<YQTimezoneSelectorPrivate::Location>::const_iterator it = d->locations.begin(); it != d->locations.end(); ++it )
385 if ( ( *it ).zone == zone )
390 d->_zoom = d->_best.pix_pos;
394 d->cachePix = QPixmap();
402 void YQTimezoneSelector::slotBlink()
404 if ( d->_best.zone.isNull() )
410 if ( d->highlight++ > 9 )
413 QPoint current = d->pixToWindow( d->_best.pix_pos );
414 update( QRect( current - QPoint( 3, 3 ), QSize( 7, 7 ) ) );