22 #include "holidayregionselector.h"
23 #include "ui_holidayregionselector.h"
25 #include "holidayregion.h"
29 #include <KLocalizedString>
33 using namespace KHolidays;
35 class HolidayRegionSelector::Private
38 Private( HolidayRegionSelector *q_ptr )
56 void clearSelection();
64 void setItemRegionUseFlags(
QTreeWidgetItem *item, RegionUseFlags regionUseFlags );
65 HolidayRegionSelector::RegionUseFlags itemRegionUseFlags(
QTreeWidgetItem *item );
67 QAbstractItemView::SelectionMode m_selectionMode;
68 bool m_enableRegionUseFlags;
71 Ui::holidayRegionSelector m_ui;
72 HolidayRegionSelector *q;
75 void HolidayRegionSelector::Private::clearSelection()
77 m_ui.regionTreeWidget->blockSignals(
true );
80 ( *it )->setCheckState( Private::SelectColumn, Qt::Unchecked );
81 ( *it )->setData( Private::SelectColumn, Qt::UserRole, RegionAvailable );
82 itemCombo( ( *it ) )->setCurrentIndex( 0 );
83 ( *it )->setData( Private::ComboColumn, Qt::UserRole, NotUsed );
86 m_ui.regionTreeWidget->blockSignals(
false );
93 if ( ( *it )->data( HolidayRegionSelector::Private::RegionColumn, Qt::UserRole ) ==
102 void HolidayRegionSelector::Private::initItem(
QTreeWidgetItem *listItem, HolidayRegion *region )
104 m_ui.regionTreeWidget->blockSignals(
true );
105 QString languageName = KGlobal::locale()->languageCodeToName( region->languageCode() );
106 listItem->
setCheckState( Private::SelectColumn, Qt::Unchecked );
107 QString text = i18n(
"<p>Select to use Holiday Region</p>" );
108 listItem->
setToolTip( Private::SelectColumn, text );
109 listItem->
setToolTip( Private::ComboColumn, text );
110 text = i18n(
"<p>Select to use Holiday Region</p>" );
111 listItem->
setToolTip( Private::SelectColumn, text );
112 listItem->
setToolTip( Private::ComboColumn, text );
113 listItem->
setText( Private::RegionColumn, region->name() );
114 QString toolTip = i18n(
"<p><b>Region:</b> %1<br/>"
115 "<b>Language:</b> %2<br/>"
116 "<b>Description:</b> %3</p>",
117 region->name(), languageName, region->description() );
118 listItem->
setToolTip( Private::RegionColumn, toolTip );
119 listItem->
setData( Private::RegionColumn, Qt::UserRole, region->regionCode() );
120 listItem->
setText( Private::LanguageColumn, languageName );
121 listItem->
setData( Private::LanguageColumn, Qt::UserRole, region->languageCode() );
122 listItem->
setText( Private::DescriptionColumn, region->description() );
123 listItem->
setToolTip( Private::DescriptionColumn, region->description() );
124 KComboBox *combo =
new KComboBox();
125 combo->setAutoFillBackground(
true );
126 QString comboText = i18n(
"<p>You can choose to display the Holiday Region for information only, "
127 "or to use the Holiday Region when displaying or calculating days off "
128 "such as Public Holidays. If you choose to use the Holiday Region for "
129 "Days Off, then only those Holiday Events marked in the Holiday Region "
130 "as Days Off will be used for non-work days, Holiday Events that are "
131 "not marked in the Holiday Region as Days Off will continue to be "
133 combo->setToolTip( comboText );
134 comboText = i18nc(
"Combobox label, Holiday Region not used",
"Not Used" );
135 combo->addItem( comboText,
QVariant( NotUsed ) );
136 comboText = i18nc(
"Combobox label, use Holiday Region for information only",
"Information" );
137 combo->addItem( comboText,
QVariant( UseInformationOnly ) );
138 comboText = i18nc(
"Combobox label, use Holiday Region for days off",
"Days Off" );
139 combo->addItem( comboText,
QVariant( UseDaysOff ) );
140 combo->setCurrentIndex( 0 );
141 listItem->
setData( Private::ComboColumn, Qt::UserRole, NotUsed );
142 m_ui.regionTreeWidget->setItemWidget( listItem, ComboColumn, combo );
143 connect( combo, SIGNAL(currentIndexChanged(
int)),
144 q, SLOT(itemChanged(
int)) );
145 m_ui.regionTreeWidget->blockSignals(
false );
150 return item->
data( Private::RegionColumn, Qt::UserRole ).
toString();
155 return item->
data( Private::LanguageColumn, Qt::UserRole ).
toString();
158 HolidayRegionSelector::SelectionStatus
159 HolidayRegionSelector::Private::itemSelectionStatus(
QTreeWidgetItem *item )
162 ( HolidayRegionSelector::SelectionStatus )
163 item->
data( Private::SelectColumn, Qt::UserRole ).
toInt();
166 void HolidayRegionSelector::Private::setItemRegionUseFlags(
QTreeWidgetItem *item,
167 RegionUseFlags regionUseFlags )
170 item->
setData( ComboColumn, Qt::UserRole,
QVariant( regionUseFlags ) );
171 if ( regionUseFlags & UseDaysOff ) {
172 setItemStatus( item, RegionSelected );
173 itemCombo( item )->setCurrentIndex( 2 );
174 }
else if ( regionUseFlags & UseInformationOnly ) {
175 setItemStatus( item, RegionSelected );
176 itemCombo( item )->setCurrentIndex( 1 );
178 setItemStatus( item, RegionAvailable );
179 itemCombo( item )->setCurrentIndex( 0 );
183 HolidayRegionSelector::RegionUseFlags
184 HolidayRegionSelector::Private::itemRegionUseFlags(
QTreeWidgetItem *item )
186 return (RegionUseFlags) item->
data( ComboColumn, Qt::UserRole ).
toInt();
189 KComboBox *HolidayRegionSelector::Private::itemCombo(
QTreeWidgetItem *item )
191 return static_cast<KComboBox*
>( m_ui.regionTreeWidget->itemWidget( item, ComboColumn ) );
194 void HolidayRegionSelector::Private::setItemStatus(
QTreeWidgetItem *item, SelectionStatus status )
196 if ( m_selectionMode == QAbstractItemView::SingleSelection &&
197 status == RegionSelected ) {
201 Qt::CheckState selectStatus = Qt::Unchecked;
203 bool disabled =
false;
213 case RegionAvailable:
217 if ( m_selectionMode != QAbstractItemView::NoSelection ) {
218 selectStatus = Qt::Checked;
226 m_ui.regionTreeWidget->blockSignals(
true );
227 item->
setData( Private::SelectColumn, Qt::UserRole, status );
231 m_ui.regionTreeWidget->blockSignals(
false );
234 HolidayRegionSelector::HolidayRegionSelector(
QWidget *parent ) :
235 QWidget( parent ), d( new Private( this ) )
240 d->m_ui.regionTreeWidget->setColumnCount( 5 );
242 QString header = i18nc(
"Header for Select column",
"Select" );
243 QString text = i18n(
"<p>This column selects to use the Holiday Region</p>" );
244 headerItem->
setText( Private::SelectColumn, header );
245 headerItem->
setToolTip( Private::SelectColumn, text );
246 headerItem->
setWhatsThis( Private::SelectColumn, text );
247 headerItem->
setText( Private::ComboColumn, header );
248 headerItem->
setToolTip( Private::ComboColumn, text );
250 header = i18nc(
"Header for Holiday Region column",
"Region" );
251 text = i18n(
"<p>This column displays the name of the Holiday Region</p>" );
252 headerItem->
setText( Private::RegionColumn, header );
253 headerItem->
setToolTip( Private::RegionColumn, text );
254 headerItem->
setWhatsThis( Private::RegionColumn, text );
255 header = i18nc(
"Header for Language column",
"Language" );
256 text = i18n(
"<p>This column displays the language of the Holiday Region</p>" );
257 headerItem->
setText( Private::LanguageColumn, header );
258 headerItem->
setToolTip( Private::LanguageColumn, text );
259 headerItem->
setWhatsThis( Private::LanguageColumn, text );
260 header = i18nc(
"Header for Description column",
"Description" );
261 text = i18n(
"<p>This column displays the description of the Holiday Region</p>" );
262 headerItem->
setText( Private::DescriptionColumn, header );
263 headerItem->
setToolTip( Private::DescriptionColumn, text );
264 headerItem->
setWhatsThis( Private::DescriptionColumn, text );
266 d->m_ui.regionTreeWidget->setSelectionMode( QAbstractItemView::NoSelection );
267 d->m_ui.regionTreeWidget->setItemsExpandable(
true );
268 d->m_ui.regionTreeWidget->setUniformRowHeights(
true );
269 d->m_ui.regionTreeWidget->setAllColumnsShowFocus(
true );
270 connect( d->m_ui.regionTreeWidget, SIGNAL(itemChanged(
QTreeWidgetItem*,
int)),
275 foreach (
const QString ®ionCode, HolidayRegion::regionCodes() ) {
276 regionMap[regionCode] =
new HolidayRegion( regionCode );
277 QString country = regionMap[regionCode]->countryCode().split(
QLatin1Char(
'-') ).at( 0 );
278 regionCodeMap[country].append( regionCode );
282 while ( it.hasNext() ) {
288 if ( it.value().count() == 1 && regionMap[it.value().at( 0 )]->countryCode() == country ) {
289 d->initItem( rootItem, regionMap[ it.value().at( 0 ) ] );
291 rootItem->
setText( Private::RegionColumn,
292 KGlobal::locale()->countryCodeToName( country ) );
293 d->m_ui.regionTreeWidget->setFirstItemColumnSpanned ( rootItem,
true );
294 foreach (
const QString ®ionCode, it.value() ) {
296 d->initItem( childItem, regionMap[ regionCode ] );
302 while ( i.hasNext() ) {
307 d->m_ui.regionTreeWidget->expandAll();
308 d->m_ui.regionTreeWidget->sortItems( Private::RegionColumn, Qt::AscendingOrder );
309 d->m_ui.regionTreeWidget->resizeColumnToContents( Private::SelectColumn );
310 d->m_ui.regionTreeWidget->resizeColumnToContents( Private::ComboColumn );
311 d->m_ui.regionTreeWidget->resizeColumnToContents( Private::RegionColumn );
312 d->m_ui.regionTreeWidget->resizeColumnToContents( Private::LanguageColumn );
315 d->m_ui.searchLineWidget->searchLine()->addTreeWidget( d->m_ui.regionTreeWidget );
318 setSelectionMode( QAbstractItemView::MultiSelection );
319 setRegionUseFlagsEnabled(
true );
322 HolidayRegionSelector::~HolidayRegionSelector()
327 QStringList HolidayRegionSelector::holidayRegions()
const
329 return d->m_holidayRegionCodes;
332 void HolidayRegionSelector::setSelectionMode( QAbstractItemView::SelectionMode selectionMode )
334 d->m_selectionMode = selectionMode;
336 if ( selectionMode == QAbstractItemView::NoSelection ) {
337 setRegionUseFlagsEnabled(
false );
341 QAbstractItemView::SelectionMode HolidayRegionSelector::selectionMode()
const
343 return d->m_selectionMode;
346 void HolidayRegionSelector::setRegionUseFlagsEnabled(
bool enableRegionUseFlags )
348 d->m_enableRegionUseFlags = enableRegionUseFlags;
349 d->m_ui.regionTreeWidget->setColumnHidden( Private::SelectColumn, enableRegionUseFlags );
350 d->m_ui.regionTreeWidget->setColumnHidden( Private::ComboColumn, !enableRegionUseFlags );
353 bool HolidayRegionSelector::regionUseFlagsEnabled()
const
355 return d->m_enableRegionUseFlags;
358 void HolidayRegionSelector::setSelectionStatus(
const QString &holidayRegionCode,
359 HolidayRegionSelector::SelectionStatus status )
363 d->setItemStatus( item, status );
367 HolidayRegionSelector::SelectionStatus
368 HolidayRegionSelector::selectionStatus(
const QString &holidayRegionCode )
const
372 return d->itemSelectionStatus( item );
374 return HolidayRegionSelector::RegionHidden;
378 HolidayRegionSelector::selectionStatus()
const
383 selection.
insert( d->itemRegion( ( *it ) ), d->itemSelectionStatus( ( *it ) ) );
390 HolidayRegionSelector::selection( HolidayRegionSelector::SelectionStatus selectionStatus )
const
392 if ( selectionMode() == QAbstractItemView::NoSelection ) {
399 if ( d->itemSelectionStatus( ( *it ) ) == selectionStatus ) {
400 selection.
append( d->itemRegion( ( *it ) ) );
408 HolidayRegionSelector::selection( HolidayRegionSelector::RegionUseFlags regionUseFlags )
const
410 if ( selectionMode() == QAbstractItemView::NoSelection ) {
417 if ( d->itemRegionUseFlags( ( *it ) ) & regionUseFlags ) {
418 selection.
append( d->itemRegion( ( *it ) ) );
426 HolidayRegionSelector::setRegionUseFlags(
const QString &holidayRegionCode,
427 HolidayRegionSelector::RegionUseFlags regionUseFlags )
431 d->setItemRegionUseFlags( item, regionUseFlags );
435 HolidayRegionSelector::RegionUseFlags
436 HolidayRegionSelector::regionUseFlags(
const QString &holidayRegionCode )
const
440 return d->itemRegionUseFlags( item );
442 return HolidayRegionSelector::NotUsed;
450 selection.
insert( d->itemRegion( ( *it ) ), d->itemRegionUseFlags( ( *it ) ) );
456 void HolidayRegionSelector::itemChanged(
QTreeWidgetItem *item,
int column )
458 if ( column == Private::SelectColumn ) {
459 if ( item->
checkState( Private::SelectColumn ) == Qt::Unchecked ) {
460 d->setItemStatus( item, RegionAvailable );
462 d->setItemStatus( item, RegionSelected );
465 emit selectionChanged();
469 void HolidayRegionSelector::itemChanged(
int index )
471 KComboBox *combo =
static_cast<KComboBox*
>( sender() );
474 while ( *it && !found ) {
475 if ( d->itemCombo( ( *it ) ) == combo ) {
477 Private::ComboColumn, Qt::UserRole, d->itemCombo( ( *it ) )->itemData( index ) );
478 if ( d->itemRegionUseFlags( ( *it ) ) == NotUsed ) {
479 d->setItemStatus( ( *it ), RegionAvailable );
481 d->setItemStatus( ( *it ), RegionSelected );
489 void HolidayRegionSelector::clearSelection()
494 void HolidayRegionSelector::setLanguageFilter(
const QStringList &languages )
499 if ( !d->m_languageFilter.contains(
500 ( *it )->data( Private::LanguageColumn, Qt::UserRole ).toString() ) ) {
501 d->setItemStatus( ( *it ), RegionAvailable );
506 d->m_languageFilter = languages;
509 QString language = ( *it2 )->
data( Private::LanguageColumn, Qt::UserRole ).toString();
510 if ( d->m_languageFilter.contains( language ) ) {
512 if ( ( *it2 )->parent() &&
513 ( *it2 )->parent() != d->m_ui.regionTreeWidget->invisibleRootItem() ) {
514 d->setItemStatus( ( *it2 )->parent(), RegionAvailable );
517 d->setItemStatus( ( *it2 ), RegionHidden );
523 QStringList HolidayRegionSelector::languageFilter()
const
525 return d->m_languageFilter;
528 void HolidayRegionSelector::setSearchHidden(
bool hideSearch )
530 d->m_ui.searchLineWidget->setHidden( hideSearch );
533 bool HolidayRegionSelector::searchHidden()
const
535 return d->m_ui.searchLineWidget->isHidden();
538 void HolidayRegionSelector::setLanguageHidden(
bool hideLanguage )
540 d->m_ui.regionTreeWidget->setColumnHidden( Private::LanguageColumn, hideLanguage );
543 bool HolidayRegionSelector::languageHidden()
const
545 return d->m_ui.regionTreeWidget->isColumnHidden( Private::LanguageColumn );
548 void HolidayRegionSelector::setDescriptionHidden(
bool hideDescription )
550 d->m_ui.regionTreeWidget->setColumnHidden( Private::DescriptionColumn, hideDescription );
553 bool HolidayRegionSelector::descriptionHidden()
const
555 return d->m_ui.regionTreeWidget->isColumnHidden( Private::DescriptionColumn );
iterator insert(const Key &key, const T &value)
void append(const T &value)
int toInt(bool *ok) const
void setText(int column, const QString &text)