void ProgressBar::update(int value) { // check when animated if we are at 100% and this is this the first time we are at 100% // so we show the progress bar only one time at the end if (!animated && (value != max || (value == max && last_value == max))) return; int now = time(NULL); // check whether the time since the last show and the change was big enough to show a progress double percentage = value / (double) max * 100.; if (last_update + 1 > now && !(last_percentage != max && value == max)) return; // now calculate the current and average speed double speed = (double) (value - last_value) / (now - last_update); double average_speed = (double) value / (now - start); if (value == max) // use the average speed at the end speed = average_speed; // try to determine the width of the terminal // use 80 columns as default if we can't determine a terminal size int terminal_width = 80; #ifdef TIOCGWINSZ struct winsize ws = {0, 0, 0, 0}; ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws); if (ws.ws_col != 0) terminal_width = ws.ws_col; #endif // create the progress stats: percentage, current/maximum value, speed, eta std::string stats; // show eta only when progress is not finished, and only when speed != 0 // (speed = 0 can sometimes happen at the begin of rendering) if (value != max && speed != 0) { int eta = (max - value) / average_speed; stats = createProgressStats(percentage, value, max, speed, eta); } else { stats = createProgressStats(percentage, value, max, speed); } // now create the progress bar // with the remaining size minus one as size // (because the space between progress and stats) int progressbar_width = terminal_width - stats.size() - 1; std::string progressbar = createProgressBar(progressbar_width, percentage); // go to the begin of the line and clear it if (animated) { std::cout << "\r"; for (int i = 0; i < last_output_len; i++) std::cout << " "; std::cout << "\r"; } // now show everything std::cout << progressbar << " " << stats; if (animated) { std::cout << "\r"; std::cout.flush(); } else std::cout << std::endl; // set this as last shown last_update = now; last_value = value; last_percentage = percentage; last_output_len = progressbar.size() + 1 + stats.size(); }
//! [0] WidgetGallery::WidgetGallery(QWidget *parent) : QDialog(parent) { originalPalette = QApplication::palette(); styleComboBox = new QComboBox; styleComboBox->addItem("NorwegianWood"); styleComboBox->addItems(QStyleFactory::keys()); styleLabel = new QLabel(tr("&Style:")); styleLabel->setBuddy(styleComboBox); useStylePaletteCheckBox = new QCheckBox(tr("&Use style's standard palette")); useStylePaletteCheckBox->setChecked(true); disableWidgetsCheckBox = new QCheckBox(tr("&Disable widgets")); createTopLeftGroupBox(); createTopRightGroupBox(); createBottomLeftTabWidget(); createBottomRightGroupBox(); createProgressBar(); //! [0] //! [1] connect(styleComboBox, SIGNAL(activated(QString)), //! [1] //! [2] this, SLOT(changeStyle(QString))); connect(useStylePaletteCheckBox, SIGNAL(toggled(bool)), this, SLOT(changePalette())); connect(disableWidgetsCheckBox, SIGNAL(toggled(bool)), topLeftGroupBox, SLOT(setDisabled(bool))); connect(disableWidgetsCheckBox, SIGNAL(toggled(bool)), topRightGroupBox, SLOT(setDisabled(bool))); connect(disableWidgetsCheckBox, SIGNAL(toggled(bool)), bottomLeftTabWidget, SLOT(setDisabled(bool))); connect(disableWidgetsCheckBox, SIGNAL(toggled(bool)), bottomRightGroupBox, SLOT(setDisabled(bool))); //! [2] //! [3] QHBoxLayout *topLayout = new QHBoxLayout; //! [3] //! [4] topLayout->addWidget(styleLabel); topLayout->addWidget(styleComboBox); topLayout->addStretch(1); topLayout->addWidget(useStylePaletteCheckBox); topLayout->addWidget(disableWidgetsCheckBox); QGridLayout *mainLayout = new QGridLayout; mainLayout->addLayout(topLayout, 0, 0, 1, 2); mainLayout->addWidget(topLeftGroupBox, 1, 0); mainLayout->addWidget(topRightGroupBox, 1, 1); mainLayout->addWidget(bottomLeftTabWidget, 2, 0); mainLayout->addWidget(bottomRightGroupBox, 2, 1); mainLayout->addWidget(progressBar, 3, 0, 1, 2); mainLayout->setRowStretch(1, 1); mainLayout->setRowStretch(2, 1); mainLayout->setColumnStretch(0, 1); mainLayout->setColumnStretch(1, 1); setLayout(mainLayout); setWindowTitle(tr("Styles")); changeStyle("NorwegianWood"); }
/** * Lay out the widgets (portrait mode). */ void HomeScreen::setupUI() { // Get the handle to the main layout and the screen. mMainLayout = getMainLayout(); mScreen = getScreen(); mLabel = getTopLabel(); mSearchButton = getTopButtonRight(); // We do not need a Back button in this screen, // so we can dismiss it. MAWidgetHandle backBtn = getTopButtonLeft(); maWidgetDestroy(backBtn); // The creation of the main layout is already done in the base class // constructor, called at derived object creation. // So, we can use a handle for the main layout at any point. // Set the text for the button widget in the top layout. setButtonText(mSearchButton, " NEXT "); // Add a label before the Progress bar. mProgressLabel = createLabel( mScreenWidth, " Please wait...", DARK_GREY, mFontSize ); // Show it only after Search is pressed. maWidgetSetProperty(mProgressLabel,MAW_WIDGET_VISIBLE, "false"); maWidgetAddChild(mMainLayout, mProgressLabel); // Create a progress bar for the Search action. mProgressBar = createProgressBar(); // Set the range of the progress bar from 0..100 setWidgetProperty( mProgressBar, MAW_PROGRESS_BAR_MAX, PROGRESS_BAR_MAX_VALUE); // Set the progress value to 0. setWidgetProperty( mProgressBar, MAW_PROGRESS_BAR_PROGRESS, 0); // Hide the widget at first, and display it when a Search is being performed. maWidgetSetProperty( mProgressBar, MAW_WIDGET_VISIBLE, "false"); maWidgetAddChild(mMainLayout, mProgressBar); // Next, fill the remaining space with an edit box and // some check boxes for the categories. MAWidgetHandle hintLabel = createLabel( mScreenWidth, MESSAGE_EDITBOX_HINT.c_str(), BLUE, mFontSize); maWidgetSetProperty( hintLabel,MAW_LABEL_TEXT_HORIZONTAL_ALIGNMENT,MAW_ALIGNMENT_LEFT); maWidgetAddChild(mMainLayout, hintLabel); // Add only one edit box. mEditBox = createEditBox(mScreenWidth, MAW_CONSTANT_WRAP_CONTENT); maWidgetAddChild(mMainLayout, mEditBox); // Add a small spacer before the categories. maWidgetAddChild(mMainLayout, createSpacer(mScreenWidth, mPaddingSize)); // Add the layout with checkable categories. maWidgetAddChild( mMainLayout, createCategoriesLayout()); // Add a label for the slider. MAWidgetHandle sliderLabel = createLabel( mScreenWidth, " Please select the results limit. ", BLUE,mFontSize); maWidgetAddChild(mMainLayout, sliderLabel); // Create a slider control for selecting the desired number of results. mSlider = createSlider(); setWidgetProperty(mSlider, MAW_SLIDER_MAX, SLIDER_MAX_VALUE); // Set the current slider value to 10. setWidgetProperty(mSlider, MAW_SLIDER_VALUE, 10); maWidgetAddChild(mMainLayout, mSlider); // Add two labels with minimum and maximum value of the slider. MAWidgetHandle valuesLayout = maWidgetCreate(MAW_HORIZONTAL_LAYOUT); MAWidgetHandle minValue = createLabel( mScreenWidth / 2, " 0 ", DARK_GREY, mFontSize); maWidgetSetProperty(minValue, MAW_LABEL_TEXT_HORIZONTAL_ALIGNMENT, MAW_ALIGNMENT_LEFT); maWidgetAddChild(valuesLayout, minValue); MAWidgetHandle maxValue = createLabel( mScreenWidth / 2, "", DARK_GREY, mFontSize); setWidgetProperty(maxValue, MAW_LABEL_TEXT, SLIDER_MAX_VALUE); maWidgetSetProperty(maxValue, MAW_LABEL_TEXT_HORIZONTAL_ALIGNMENT, MAW_ALIGNMENT_RIGHT); maWidgetAddChild(valuesLayout, maxValue); // Add this layout to the main one. maWidgetAddChild(mMainLayout, valuesLayout); }