| 
									
										
										
										
											2019-03-25 04:25:25 +01:00
										 |  |  | #include <AK/StringBuilder.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  | #include <LibGUI/GPainter.h>
 | 
					
						
							|  |  |  | #include <LibGUI/GProgressBar.h>
 | 
					
						
							| 
									
										
										
										
											2019-03-22 02:49:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | GProgressBar::GProgressBar(GWidget* parent) | 
					
						
							| 
									
										
										
										
											2019-03-28 20:15:13 +01:00
										 |  |  |     : GFrame(parent) | 
					
						
							| 
									
										
										
										
											2019-03-22 02:49:14 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-10 03:43:46 +02:00
										 |  |  |     set_frame_shape(FrameShape::Container); | 
					
						
							|  |  |  |     set_frame_shadow(FrameShadow::Sunken); | 
					
						
							| 
									
										
										
										
											2019-03-28 20:15:13 +01:00
										 |  |  |     set_frame_thickness(2); | 
					
						
							| 
									
										
										
										
											2019-03-22 02:49:14 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GProgressBar::~GProgressBar() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GProgressBar::set_value(int value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_value == value) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     m_value = value; | 
					
						
							|  |  |  |     update(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GProgressBar::set_range(int min, int max) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ASSERT(min < max); | 
					
						
							|  |  |  |     m_min = min; | 
					
						
							|  |  |  |     m_max = max; | 
					
						
							|  |  |  |     if (m_value > m_max) | 
					
						
							|  |  |  |         m_value = m_max; | 
					
						
							|  |  |  |     if (m_value < m_min) | 
					
						
							|  |  |  |         m_value = m_min; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GProgressBar::paint_event(GPaintEvent& event) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-03-28 20:15:13 +01:00
										 |  |  |     GFrame::paint_event(event); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-28 17:19:56 +01:00
										 |  |  |     GPainter painter(*this); | 
					
						
							| 
									
										
										
										
											2019-03-28 20:15:13 +01:00
										 |  |  |     auto rect = frame_inner_rect(); | 
					
						
							| 
									
										
										
										
											2019-03-29 15:01:54 +01:00
										 |  |  |     painter.add_clip_rect(rect); | 
					
						
							|  |  |  |     painter.add_clip_rect(event.rect()); | 
					
						
							| 
									
										
										
										
											2019-03-22 02:49:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 06:08:06 +02:00
										 |  |  |     String progress_text; | 
					
						
							|  |  |  |     if (m_format != Format::NoText) { | 
					
						
							|  |  |  |         // Then we draw the progress text over the gradient.
 | 
					
						
							|  |  |  |         // We draw it twice, once offset (1, 1) for a drop shadow look.
 | 
					
						
							|  |  |  |         StringBuilder builder; | 
					
						
							|  |  |  |         builder.append(m_caption); | 
					
						
							| 
									
										
										
										
											2019-08-14 20:31:46 +02:00
										 |  |  |         if (m_format == Format::Percentage) { | 
					
						
							|  |  |  |             float range_size = m_max - m_min; | 
					
						
							|  |  |  |             float progress = (m_value - m_min) / range_size; | 
					
						
							| 
									
										
										
										
											2019-04-11 06:08:06 +02:00
										 |  |  |             builder.appendf("%d%%", (int)(progress * 100)); | 
					
						
							| 
									
										
										
										
											2019-08-14 20:31:46 +02:00
										 |  |  |         } else if (m_format == Format::ValueSlashMax) { | 
					
						
							| 
									
										
										
										
											2019-04-11 06:08:06 +02:00
										 |  |  |             builder.appendf("%d/%d", m_value, m_max); | 
					
						
							| 
									
										
										
										
											2019-08-14 20:31:46 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-04-11 06:08:06 +02:00
										 |  |  |         progress_text = builder.to_string(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-22 02:49:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-14 20:31:46 +02:00
										 |  |  |     StylePainter::paint_progress_bar(painter, rect, m_min, m_max, m_value, progress_text); | 
					
						
							| 
									
										
										
										
											2019-03-22 02:49:14 +01:00
										 |  |  | } |