Example #1
0
void sysLocale::sSave()
{
  if (_code->text().trimmed().length() == 0)
  {
    QMessageBox::critical( this, tr("Cannot Save Locale"),
                           tr("<p>You must enter a Code for this Locale before "
                              "you may save it.") );
    _code->setFocus();
    return;
  }

  q.prepare( "SELECT locale_id "
             "FROM locale "
             "WHERE ( (locale_id<>:locale_id)"
             " AND (UPPER(locale_code)=UPPER(:locale_code)) );");
  q.bindValue(":locale_id", _localeid);
  q.bindValue(":locale_code", _code->text().trimmed());
  q.exec();
  if (q.first())
  {
    QMessageBox::critical( this, tr("Cannot Create Locale"),
			   tr( "A Locale with the entered code already exists."
			       "You may not create a Locale with this code." ) );
    _code->setFocus();
    return;
  }
  else if (q.lastError().type() != QSqlError::NoError)
  {
    systemError(this, q.lastError().databaseText(), __FILE__, __LINE__);
    return;
  }

  QLocale sampleLocale = generateLocale();

  if (_mode == cNew)
  {
    q.prepare( "INSERT INTO locale "
               "( locale_id, locale_code, locale_descrip,"
               "  locale_lang_id, locale_country_id, "
               "  locale_dateformat, locale_timeformat, locale_timestampformat,"
	       "  locale_intervalformat, locale_qtyformat,"
               "  locale_curr_scale,"
               "  locale_salesprice_scale, locale_purchprice_scale,"
               "  locale_extprice_scale, locale_cost_scale,"
               "  locale_qty_scale, locale_qtyper_scale,"
               "  locale_uomratio_scale, locale_percent_scale, "
               "  locale_comments, "
               "  locale_error_color, locale_warning_color,"
               "  locale_emphasis_color, locale_altemphasis_color,"
               "  locale_expired_color, locale_future_color) "
               "VALUES "
               "( :locale_id, :locale_code, :locale_descrip,"
               "  :locale_lang_id, :locale_country_id,"
               "  :locale_dateformat, :locale_timeformat, :locale_timestampformat,"
	       "  :locale_intervalformat, :locale_qtyformat,"
               "  :locale_curr_scale,"
               "  :locale_salesprice_scale, :locale_purchprice_scale,"
               "  :locale_extprice_scale, :locale_cost_scale,"
               "  :locale_qty_scale, :locale_qtyper_scale,"
               "  :locale_uomratio_scale, :local_percent_scale, "
               "  :locale_comments,"
               "  :locale_error_color, :locale_warning_color,"
               "  :locale_emphasis_color, :locale_altemphasis_color,"
               "  :locale_expired_color, :locale_future_color);" );
  }
  else if ( (_mode == cEdit) || (_mode == cCopy) )
    q.prepare( "UPDATE locale "
                "SET locale_code=:locale_code,"
                "    locale_descrip=:locale_descrip,"
                "    locale_lang_id=:locale_lang_id,"
                "    locale_country_id=:locale_country_id,"
                "    locale_dateformat=:locale_dateformat,"
                "    locale_timeformat=:locale_timeformat,"
                "    locale_timestampformat=:locale_timestampformat,"
                "    locale_intervalformat=:locale_intervalformat,"
                "    locale_qtyformat=:locale_qtyformat,"
		"    locale_curr_scale=:locale_curr_scale,"
                "    locale_salesprice_scale=:locale_salesprice_scale,"
                "    locale_purchprice_scale=:locale_purchprice_scale,"
                "    locale_extprice_scale=:locale_extprice_scale,"
                "    locale_cost_scale=:locale_cost_scale,"
                "    locale_qty_scale=:locale_qty_scale,"
                "    locale_qtyper_scale=:locale_qtyper_scale,"
                "    locale_uomratio_scale=:locale_uomratio_scale,"
                "    locale_percent_scale=:locale_percent_scale,"
                "    locale_comments=:locale_comments,"
                "    locale_error_color=:locale_error_color,"
                "    locale_warning_color=:locale_warning_color,"
                "    locale_emphasis_color=:locale_emphasis_color,"
                "    locale_altemphasis_color=:locale_altemphasis_color,"
                "    locale_expired_color=:locale_expired_color,"
                "    locale_future_color=:locale_future_color "
                "WHERE (locale_id=:locale_id);" );

  q.bindValue(":locale_id",                _localeid);
  q.bindValue(":locale_code",              _code->text());
  q.bindValue(":locale_descrip",           _description->text());
  q.bindValue(":locale_lang_id",           _language->id());
  q.bindValue(":locale_country_id",        _country->id());
  q.bindValue(":locale_curr_scale",        _currencyScale->text());
  q.bindValue(":locale_salesprice_scale",  _salesPriceScale->text());
  q.bindValue(":locale_purchprice_scale",  _purchPriceScale->text());
  q.bindValue(":locale_extprice_scale",    _extPriceScale->text());
  q.bindValue(":locale_cost_scale",        _costScale->text());
  q.bindValue(":locale_qty_scale",         _qtyScale->text());
  q.bindValue(":locale_qtyper_scale",      _qtyPerScale->text());
  q.bindValue(":locale_uomratio_scale",    _uomRatioScale->text());
  q.bindValue(":locale_percent_scale",     _percentScale->text());
  q.bindValue(":locale_comments",          _comments->toPlainText());
  q.bindValue(":locale_error_color",       _error->text());
  q.bindValue(":locale_warning_color",     _warning->text());
  q.bindValue(":locale_emphasis_color",    _emphasis->text());
  q.bindValue(":locale_altemphasis_color", _alternate->text());
  q.bindValue(":locale_expired_color",     _expired->text());
  q.bindValue(":locale_future_color",      _future->text());
  q.bindValue(":locale_dateformat",     convert(sampleLocale.dateFormat(QLocale::ShortFormat)));
  q.bindValue(":locale_timeformat",     convert(sampleLocale.timeFormat(QLocale::ShortFormat)));
  q.bindValue(":locale_timestampformat",convert(sampleLocale.dateFormat(QLocale::ShortFormat)) +
                                  " " + convert(sampleLocale.timeFormat(QLocale::ShortFormat)));
  q.bindValue(":locale_intervalformat", convert(sampleLocale.timeFormat(QLocale::ShortFormat).remove("ap", Qt::CaseInsensitive)));
  q.bindValue(":locale_qtyformat",      QString(sampleLocale.decimalPoint()) +
                                        QString(sampleLocale.negativeSign()) +
                                        QString(sampleLocale.groupSeparator()));
  q.exec();
  if (q.lastError().type() != QSqlError::NoError)
  {
    systemError(this, q.lastError().databaseText(), __FILE__, __LINE__);
    return;
  }

  done(_localeid);
}
Example #2
0
	/// \brief Converts string containing value and (prefix+)unit to double (Counterpart to Helper::valueToString).
	/// \param text The text containing the value and its unit.
	/// \param unit The base unit of the value.
	/// \param ok Pointer to a success-flag, true on success, false on error.
	/// \return Decoded value.
	double stringToValue(const QString &text, Unit unit, bool *ok) {
		// Check if the text is empty
		int totalSize = text.size();
		if(!totalSize){
			if(ok)
				*ok = false;
			return 0.0;
		}
		
		// Split value and unit apart
		int valueSize = 0;
		QLocale locale;
		bool decimalFound = false;
		bool exponentFound = false;
		if(text[valueSize] == locale.negativeSign())
			++valueSize;
		for(; valueSize < text.size(); ++valueSize) {
			QChar character = text[valueSize];
			
			if(character.isDigit()) {
			}
			else if(character == locale.decimalPoint() && decimalFound == false && exponentFound == false) {
				decimalFound = true;
			}
			else if(character == locale.exponential() && exponentFound == false) {
				exponentFound = true;
				if(text[valueSize + 1] == locale.negativeSign())
					++valueSize;
			}
			else {
				break;
			}
		}
		QString valueString = text.left(valueSize);
		bool valueOk = false;
		double value = valueString.toDouble(&valueOk);
		if(!valueOk) {
			if(ok)
				*ok = false;
			return value;
		}
		QString unitString = text.right(text.size() - valueSize).trimmed();
		
		if(ok)
			*ok = true;
		switch(unit) {
			case UNIT_VOLTS: {
				// Voltage string decoding
				if(unitString.startsWith('\265'))
					return value * 1e-6;
				else if(unitString.startsWith('m'))
					return value * 1e-3;
				else
					return value;
			}
			case UNIT_DECIBEL:
				// Power level string decoding
				return value;
			
			case UNIT_SECONDS:
				// Time string decoding
				if(unitString.startsWith('p'))
					return value * 1e-12;
				else if(unitString.startsWith('n'))
					return value * 1e-9;
				else if(unitString.startsWith('\265'))
					return value * 1e-6;
				else if(unitString.startsWith("min"))
					return value * 60;
				else if(unitString.startsWith('m'))
					return value * 1e-3;
				else if(unitString.startsWith('h'))
					return value * 3600;
				else
					return value;
			
			case UNIT_HERTZ:
				// Frequency string decoding
				if(unitString.startsWith('k'))
					return value * 1e3;
				else if(unitString.startsWith('M'))
					return value * 1e6;
				else if(unitString.startsWith('G'))
					return value * 1e9;
				else
					return value;
			
			case UNIT_SAMPLES:
				// Sample count string decoding
				if(unitString.startsWith('k'))
					return value * 1e3;
				else if(unitString.startsWith('M'))
					return value * 1e6;
				else if(unitString.startsWith('G'))
					return value * 1e9;
				else
					return value;
			
			default:
				if(ok)
					*ok = false;
				return value;
		}
	}