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> 69 bool operator<(
const Location& l2)
const;
74 QList<Location> locations;
76 Location findBest(
const QPoint &pos )
const;
82 QPoint pixPosition(
const Location &pos )
const;
84 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());
108 bool YQTimezoneSelectorPrivate::Location::operator<(
const Location& l1 )
const 110 return l1.latitude < latitude;
115 : QFrame( (QWidget *) parent->widgetRep() )
116 , YTimezoneSelector( parent, pixmap, timezones )
120 setWidgetRep(
this );
121 setMouseTracking(
true);
122 d->_pix.load( fromUTF8( pixmap ) );
124 setStretchable( YD_HORIZ,
true );
125 setStretchable( YD_VERT,
true );
128 FILE *tzfile = fopen (
"/usr/share/zoneinfo/zone.tab",
"r");
129 while (fgets (buf,
sizeof(buf), tzfile))
131 if (*buf ==
'#')
continue;
134 QStringList arr = sbuf.trimmed().split(
'\t' );
137 while ( split_index < arr[1].length() && arr[1][split_index] !=
'-' && arr[1][split_index] !=
'+' )
141 loc.country = arr[0];
143 std::map<std::string, std::string>::const_iterator tooltip = timezones.find( loc.zone.toStdString() );
144 if (tooltip == timezones.end() )
147 loc.tip = fromUTF8( tooltip->second );
148 if ( arr.size() > 3 )
149 loc.comment = arr[3];
150 loc.latitude = convert_pos ( arr[1].left( split_index ), 2);
151 loc.longitude = convert_pos ( arr[1].mid( split_index ), 3);
153 loc.pix_pos = d->pixPosition( loc );
155 d->locations.push_back( loc );
160 qSort( d->locations.begin(), d->locations.end() );
162 d->blink =
new QTimer(
this );
163 d->blink->setInterval( 200 );
164 connect( d->blink, &pclass(d->blink)::timeout,
165 this, &pclass(
this)::slotBlink );
191 resize( newWidth, newHeight );
195 QPoint YQTimezoneSelectorPrivate::pixPosition(
const Location &pos )
const 197 return QPoint( (
int) ( _pix.width() / 2 + _pix.width() / 2 * pos.longitude / 180 ),
198 (
int) ( _pix.height() / 2 - _pix.height() / 2 * pos.latitude / 90 ) ) ;
202 void YQTimezoneSelector::mousePressEvent ( QMouseEvent * event )
204 if ( event->button() == Qt::RightButton )
207 d->cachePix = QPixmap();
209 else if ( event->button() == Qt::LeftButton )
211 d->_best = d->findBest( event->pos() );
213 if ( d->_zoom.isNull() )
215 QPoint click =
event->pos();
217 d->_zoom.rx() = (int) (
double( click.x() ) * d->_pix.width() / width() );
218 d->_zoom.ry() = (int) (
double( click.y() ) * d->_pix.height() / height() );
221 d->cachePix = QPixmap();
234 void YQTimezoneSelector::paintEvent( QPaintEvent *event )
236 QFrame::paintEvent( event );
239 if ( d->cachePix.width() != width() || d->cachePix.height() != height() )
240 d->cachePix = QPixmap();
242 if ( d->_zoom.isNull() )
244 if ( d->cachePix.isNull() )
246 QImage t = d->_pix.scaled( width(), height(), Qt::KeepAspectRatio );
247 d->cachePix = QPixmap::fromImage( t );
249 p.drawPixmap( ( width() - d->cachePix.width() ) / 2, ( height() - d->cachePix.height() ) / 2, d->cachePix );
251 Q_INIT_RESOURCE( qt_icons );
252 QIcon icon = QIcon::fromTheme(
"zoom-in", QIcon(
":/zoom-in" ) );
254 if ( !icon.isNull() )
255 setCursor( QCursor( icon.pixmap( QSize( 16, 16 ) ) ) );
259 int left = qMin( qMax( d->_zoom.x() - width() / 2, 0 ), d->_pix.width() - width() );
260 int top = qMin( qMax( d->_zoom.y() - height() / 2, 0 ), d->_pix.height() - height() );
262 if ( d->cachePix.isNull() )
263 d->cachePix = QPixmap::fromImage( d->_pix.copy( QRect( QPoint( left, top ), size() ) ) );
265 p.drawPixmap( 0, 0, d->cachePix );
267 setCursor( Qt::CrossCursor );
270 p.setBrush( QColor(
"#D8DF57" ) );
271 p.setPen( QColor(
"#B9DFD6" ) );
273 for ( QList<YQTimezoneSelectorPrivate::Location>::const_iterator it = d->locations.begin(); it != d->locations.end(); ++it )
275 if ( !d->highlight || ( *it ).zone != d->_best.zone )
277 if ( d->_zoom.isNull() )
278 p.drawEllipse( QRect( d->pixToWindow( ( *it ).pix_pos ) - QPoint( 1,1 ), QSize( 3, 3 ) ) );
280 p.drawEllipse( QRect( d->pixToWindow( ( *it ).pix_pos ) - QPoint( 2,2 ), QSize( 5, 5 ) ) );
283 if ( d->highlight > 0 )
285 static const QColor blinks[] = { QColor(
"#00ff00" ), QColor(
"#22dd00" ), QColor(
"#44bb00" ),
286 QColor(
"#669900" ), QColor(
"#887700" ), QColor(
"#aa5500" ),
287 QColor(
"#887700" ), QColor(
"#669900" ), QColor(
"#44bb00" ),
288 QColor(
"#22dd00" ) };
289 int index = d->highlight - 1;
290 p.setPen( blinks[ index ] );
291 p.setBrush( blinks[ index ] );
293 p.drawEllipse( QRect( d->pixToWindow( d->_best.pix_pos ) - QPoint( 2,2 ), QSize( 5, 5 ) ) );
298 QFontMetrics fm( f );
300 QPoint off = d->pixToWindow( d->_best.pix_pos ) + QPoint( 11, 4 );
301 int tw = fm.width( d->_best.tip );
302 if ( tw + off.x() > width() )
303 off.rx() = d->pixToWindow( d->_best.pix_pos ).x() - tw - 10;
305 p.setPen( Qt::black );
306 p.drawText( off, d->_best.tip );
308 p.setPen( Qt::white );
309 p.drawText( off - QPoint( 1, 1 ), d->_best.tip );
316 double min_dist = 2000;
318 for ( QList<Location>::const_iterator it = locations.begin(); it != locations.end(); ++it )
320 double dist = QPoint( pixToWindow( ( *it ).pix_pos ) - pos ).manhattanLength ();
321 if ( dist < min_dist )
330 bool YQTimezoneSelector::event(QEvent *event)
332 if (event->type() == QEvent::ToolTip)
334 QHelpEvent *helpEvent =
static_cast<QHelpEvent *
>(event);
337 QToolTip::showText(helpEvent->globalPos(), best.tip );
339 return QWidget::event(event);
345 return d->_best.zone.toStdString();
348 QPoint YQTimezoneSelectorPrivate::pixToWindow(
const QPoint &pos )
const 350 if ( _zoom.isNull() )
352 return QPoint( (
int) (
double( pos.x() ) * cachePix.width() / _pix.width() ) + ( parent->width() - cachePix.width() ) / 2,
353 (
int) ( double( pos.y() ) * cachePix.height() / _pix.height() ) + ( parent->height() - cachePix.height() ) /2 );
355 int left = qMin( qMax( _zoom.x() - parent->width() / 2, 0 ), _pix.width() - parent->width() );
356 int top = qMin( qMax( _zoom.y() - parent->height() / 2, 0 ), _pix.height() - parent->height() );
358 return QPoint( pos.x() - left, pos.y() - top );
363 QString zone = fromUTF8( _zone );
365 if ( d->_best.zone == zone )
370 for ( QList<YQTimezoneSelectorPrivate::Location>::const_iterator it = d->locations.begin(); it != d->locations.end(); ++it )
372 if ( ( *it ).zone == zone )
377 d->_zoom = d->_best.pix_pos;
381 d->cachePix = QPixmap();
388 void YQTimezoneSelector::slotBlink()
390 if ( d->_best.zone.isNull() )
396 if ( d->highlight++ > 9 )
399 QPoint current = d->pixToWindow( d->_best.pix_pos );
400 update( QRect( current - QPoint( 3, 3 ), QSize( 7, 7 ) ) );
virtual int preferredHeight()
Preferred height of the widget.
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
virtual std::string currentZone() const
subclasses have to implement this to return value
YQTimezoneSelector(YWidget *parent, const std::string &pixmap, const std::map< std::string, std::string > &timezones)
Constructor.
void sendEvent(YEvent *event)
Widget event handlers (slots) call this when an event occured that should be the answer to a UserInpu...
virtual ~YQTimezoneSelector()
Destructor.
virtual int preferredWidth()
Preferred width of the widget.
virtual void setCurrentZone(const std::string &zone, bool zoom)
subclasses have to implement this to set value
static YQUI * ui()
Access the global Qt-UI.