finalcut/src/fprogressbar.cpp

261 lines
6.1 KiB
C++
Raw Normal View History

2017-11-04 07:03:53 +01:00
/***********************************************************************
* fprogressbar.cpp - Widget FProgressbar *
* *
* This file is part of the Final Cut widget toolkit *
* *
* Copyright 2014-2017 Markus Gans *
* *
* The Final Cut is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public License *
* as published by the Free Software Foundation; either version 3 of *
* the License, or (at your option) any later version. *
* *
* The Final Cut is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
***********************************************************************/
2015-05-23 13:35:12 +02:00
#include "final/fprogressbar.h"
2015-05-23 13:35:12 +02:00
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
// class FProgressbar
//----------------------------------------------------------------------
// constructors and destructor
//----------------------------------------------------------------------
2015-09-22 04:18:20 +02:00
FProgressbar::FProgressbar(FWidget* parent)
: FWidget(parent)
, percentage(-1)
, bar_length(getWidth())
2015-05-23 13:35:12 +02:00
{
2015-09-22 04:18:20 +02:00
unsetFocusable();
setShadow();
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
2017-09-11 03:06:02 +02:00
FProgressbar::~FProgressbar() // destructor
2015-09-22 04:18:20 +02:00
{ }
2015-05-23 13:35:12 +02:00
// public methods of FProgressbar
//----------------------------------------------------------------------
void FProgressbar::setPercentage (int percentage_value)
{
if ( percentage_value <= percentage )
return;
if ( percentage_value > 100 )
percentage = 100;
else if ( percentage_value < 0 )
percentage = 0;
else
percentage = percentage_value;
if ( isVisible() )
{
drawPercentage();
drawBar();
}
updateTerminal();
}
//----------------------------------------------------------------------
void FProgressbar::setGeometry (int x, int y, int w, int h, bool adjust)
{
// Set the progress bar geometry
FWidget::setGeometry (x, y, w, h, adjust);
bar_length = w;
}
//----------------------------------------------------------------------
bool FProgressbar::setShadow (bool on)
{
if ( on
2017-11-26 22:37:18 +01:00
&& term_encoding != fc::VT100
&& term_encoding != fc::ASCII )
{
flags |= fc::shadow;
setShadowSize(1,1);
}
else
{
flags &= ~fc::shadow;
setShadowSize(0,0);
}
return on;
}
//----------------------------------------------------------------------
void FProgressbar::hide()
{
int s, size;
short fg, bg;
char* blank;
FWidget* parent_widget = getParentWidget();
FWidget::hide();
if ( parent_widget )
{
fg = parent_widget->getForegroundColor();
bg = parent_widget->getBackgroundColor();
}
else
{
fg = wc.dialog_fg;
bg = wc.dialog_bg;
}
setColor (fg, bg);
s = hasShadow() ? 1 : 0;
size = getWidth() + s;
if ( size < 0 )
return;
2017-08-12 22:55:29 +02:00
try
{
blank = new char[size + 1];
2017-08-12 22:55:29 +02:00
}
catch (const std::bad_alloc& ex)
{
std::cerr << "not enough memory to alloc " << ex.what() << std::endl;
return;
}
std::memset(blank, ' ', uLong(size));
blank[size] = '\0';
for (int y = 0; y < getHeight() + s; y++)
{
setPrintPos (1, 1 + y);
print (blank);
}
delete[] blank;
setPrintPos (getWidth() - 4, 0);
print (" "); // hide percentage
}
//----------------------------------------------------------------------
void FProgressbar::reset()
{
percentage = -1;
if ( isVisible() )
{
drawPercentage();
drawBar();
}
updateTerminal();
}
2015-05-23 13:35:12 +02:00
// private methods of FProgressbar
//----------------------------------------------------------------------
void FProgressbar::draw()
{
drawPercentage();
drawBar();
if ( (flags & fc::shadow) != 0 )
drawShadow ();
flush_out();
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FProgressbar::drawPercentage()
{
FWidget* parent_widget = getParentWidget();
if ( parent_widget )
setColor ( parent_widget->getForegroundColor()
, parent_widget->getBackgroundColor() );
else
setColor ( wc.dialog_fg, wc.dialog_bg );
2015-10-11 21:56:16 +02:00
if ( isMonochron() )
setReverse(true);
setPrintPos (getWidth() - 3, 0);
2015-05-23 13:35:12 +02:00
if ( percentage < 0 || percentage > 100 )
print ("--- %");
else
printf ("%3d %%", percentage);
2015-10-11 21:56:16 +02:00
if ( isMonochron() )
setReverse(false);
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FProgressbar::drawBar()
{
int i = 0;
double length = double(bar_length * percentage) / 100;
setPrintPos (1,1);
2015-10-10 04:01:22 +02:00
setColor ( wc.progressbar_bg
, wc.progressbar_fg );
2015-05-23 13:35:12 +02:00
if ( isMonochron() )
2015-10-11 21:56:16 +02:00
setReverse(false);
2015-05-23 13:35:12 +02:00
for (; i < trunc(length); i++)
print (' ');
2015-05-23 13:35:12 +02:00
if ( isMonochron() )
2015-10-11 21:56:16 +02:00
setReverse(true);
2015-05-23 13:35:12 +02:00
if ( percentage > 0.0f && trunc(length) < bar_length )
2015-05-23 13:35:12 +02:00
{
2017-11-26 22:37:18 +01:00
if ( round(length) > trunc(length) || getMaxColor() < 16 )
2015-05-23 13:35:12 +02:00
{
if ( isMonochron() )
{
setReverse(false);
2015-10-11 21:56:16 +02:00
print (' ');
setReverse(true);
2015-05-23 13:35:12 +02:00
}
else
print (' ');
}
else
{
setColor (wc.progressbar_fg, wc.progressbar_bg);
2017-09-11 03:06:02 +02:00
print (fc::LeftHalfBlock); // ▌
2015-05-23 13:35:12 +02:00
}
2015-05-23 13:35:12 +02:00
i++;
}
setColor (wc.progressbar_fg, wc.progressbar_bg);
2015-05-23 13:35:12 +02:00
if ( getMaxColor() < 16 )
{
for (; i < bar_length; i++)
2015-05-23 13:35:12 +02:00
print (fc::MediumShade); // ▒
}
else
{
for (; i < bar_length; i++)
2015-05-23 13:35:12 +02:00
print (' ');
}
2015-10-11 21:56:16 +02:00
if ( isMonochron() )
setReverse(false);
2015-10-23 23:57:00 +02:00
2015-05-23 13:35:12 +02:00
updateTerminal();
flush_out();
}