| 
									
										
										
										
											2015-12-24 11:41:05 +00:00
										 |  |  | #include "DirectoryDelegate.hpp"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <QApplication>
 | 
					
						
							|  |  |  | #include <QVariant>
 | 
					
						
							|  |  |  | #include <QString>
 | 
					
						
							|  |  |  | #include <QStyle>
 | 
					
						
							|  |  |  | #include <QModelIndex>
 | 
					
						
							|  |  |  | #include <QPainter>
 | 
					
						
							|  |  |  | #include <QStyleOptionViewItem>
 | 
					
						
							|  |  |  | #include <QStyleOptionProgressBar>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DirectoryDelegate::paint (QPainter * painter, QStyleOptionViewItem const& option | 
					
						
							|  |  |  |                                , QModelIndex const& index) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (1 == index.column ()) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       auto progress = index.data ().toLongLong (); | 
					
						
							| 
									
										
										
										
											2021-02-18 11:56:06 +00:00
										 |  |  |       qint64 percent; | 
					
						
							| 
									
										
										
										
											2015-12-24 11:41:05 +00:00
										 |  |  |       if (progress > 0) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2021-02-18 11:56:06 +00:00
										 |  |  |           percent = int (progress * 100 / index.data (Qt::UserRole).toLongLong ()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | #if !defined (Q_OS_DARWIN)
 | 
					
						
							|  |  |  |       QStyleOptionProgressBar progress_option; | 
					
						
							|  |  |  |       auto control_element = QStyle::CE_ProgressBar; | 
					
						
							|  |  |  |       progress_option.minimum = 0; | 
					
						
							|  |  |  |       progress_option.maximum = 100; | 
					
						
							|  |  |  |       progress_option.textAlignment = Qt::AlignCenter; | 
					
						
							|  |  |  |       if (progress > 0) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           progress_option.progress = percent; | 
					
						
							|  |  |  |           progress_option.textVisible = true; | 
					
						
							| 
									
										
										
										
											2015-12-24 11:41:05 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           // not started
 | 
					
						
							| 
									
										
										
										
											2021-02-18 11:56:06 +00:00
										 |  |  |           progress_option.progress = -1; | 
					
						
							| 
									
										
										
										
											2015-12-24 11:41:05 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-02-18 11:56:06 +00:00
										 |  |  | #else
 | 
					
						
							|  |  |  |       // workaround for broken QProgressBar item delegates on macOS
 | 
					
						
							|  |  |  |       QStyleOptionViewItem progress_option; | 
					
						
							|  |  |  |       auto control_element = QStyle::CE_ItemViewItem; | 
					
						
							| 
									
										
										
										
											2021-02-18 12:14:45 +00:00
										 |  |  |       progress_option.displayAlignment = Qt::AlignHCenter; | 
					
						
							| 
									
										
										
										
											2021-02-18 11:56:06 +00:00
										 |  |  |       progress_option.index = index; | 
					
						
							|  |  |  |       progress_option.features = QStyleOptionViewItem::HasDisplay; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |       progress_option.rect = option.rect; | 
					
						
							|  |  |  |       progress_option.state = QStyle::State_Enabled; | 
					
						
							|  |  |  |       progress_option.direction = QApplication::layoutDirection (); | 
					
						
							|  |  |  |       progress_option.fontMetrics = QApplication::fontMetrics (); | 
					
						
							|  |  |  |       progress_option.text = QString::number (progress > 0 ? percent : 0) + '%'; | 
					
						
							|  |  |  |       QApplication::style ()->drawControl (control_element, &progress_option, painter); | 
					
						
							| 
									
										
										
										
											2015-12-24 11:41:05 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       QStyledItemDelegate::paint (painter, option, index); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |