| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-14 21:41:10 +01:00
										 |  |  | #include <AK/Assertions.h>
 | 
					
						
							| 
									
										
										
										
											2019-03-25 04:25:25 +01:00
										 |  |  | #include <AK/StringBuilder.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 20:33:02 +01:00
										 |  |  | #include <LibGUI/Painter.h>
 | 
					
						
							|  |  |  | #include <LibGUI/ProgressBar.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-14 23:53:11 +01:00
										 |  |  | #include <LibGfx/Palette.h>
 | 
					
						
							| 
									
										
										
										
											2019-03-22 02:49:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | namespace GUI { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ProgressBar::ProgressBar(Widget* parent) | 
					
						
							|  |  |  |     : Frame(parent) | 
					
						
							| 
									
										
										
										
											2019-03-22 02:49:14 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |     set_frame_shape(Gfx::FrameShape::Container); | 
					
						
							|  |  |  |     set_frame_shadow(Gfx::FrameShadow::Sunken); | 
					
						
							| 
									
										
										
										
											2019-03-28 20:15:13 +01:00
										 |  |  |     set_frame_thickness(2); | 
					
						
							| 
									
										
										
										
											2019-03-22 02:49:14 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | ProgressBar::~ProgressBar() | 
					
						
							| 
									
										
										
										
											2019-03-22 02:49:14 +01:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void ProgressBar::set_value(int value) | 
					
						
							| 
									
										
										
										
											2019-03-22 02:49:14 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_value == value) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     m_value = value; | 
					
						
							|  |  |  |     update(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void ProgressBar::set_range(int min, int max) | 
					
						
							| 
									
										
										
										
											2019-03-22 02:49:14 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     ASSERT(min < max); | 
					
						
							|  |  |  |     m_min = min; | 
					
						
							|  |  |  |     m_max = max; | 
					
						
							| 
									
										
										
										
											2020-01-20 13:16:58 +01:00
										 |  |  |     m_value = clamp(m_value, m_min, m_max); | 
					
						
							| 
									
										
										
										
											2019-03-22 02:49:14 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void ProgressBar::paint_event(PaintEvent& event) | 
					
						
							| 
									
										
										
										
											2019-03-22 02:49:14 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     Frame::paint_event(event); | 
					
						
							| 
									
										
										
										
											2019-03-28 20:15:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     Painter 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |     Gfx::StylePainter::paint_progress_bar(painter, rect, palette(), m_min, m_max, m_value, progress_text); | 
					
						
							| 
									
										
										
										
											2019-03-22 02:49:14 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |