25 #include <QMouseEvent>
28 #include <QStyleOption>
33 #include <knotification.h>
38 #include "kcalcdisplay.moc"
45 groupdigits_(true), twoscomplement_(true), button_(0), lit_(false),
46 num_base_(
NB_DECIMAL), precision_(9), fixed_precision_(-1), display_amount_(0),
47 history_index_(0), selection_timer_(new QTimer(this)) {
49 setFocusPolicy(Qt::StrongFocus);
51 setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
53 setBackgroundRole(QPalette::Base);
54 setForegroundRole(QPalette::Text);
55 setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
60 connect(
this, SIGNAL(
clicked()),
this, SLOT(slotDisplaySelected()));
61 connect(selection_timer_, SIGNAL(timeout()),
this, SLOT(slotSelectionTimedOut()));
78 QPalette pal = palette();
119 history_list_.insert(history_list_.begin(), output);
157 void KCalcDisplay::slotHistoryForward() {
159 if (history_list_.empty()) {
163 if (history_index_ <= 0) {
168 setAmount(history_list_[history_index_]);
175 void KCalcDisplay::slotHistoryBack() {
177 if (history_list_.empty()) {
181 if (history_index_ >= history_list_.size()) {
185 setAmount(history_list_[history_index_]);
199 str_int_ = QLatin1String(
"0");
200 str_int_exp_.clear();
242 txt.prepend(QLatin1String(
"0x"));
243 txt.remove(QLatin1Char(
' '));
246 txt.prepend(QLatin1String(
"0b"));
247 txt.remove(QLatin1Char(
' '));
250 txt.prepend(QLatin1String(
"0"));
251 txt.remove(QLatin1Char(
' '));
257 (QApplication::clipboard())->
setText(txt, QClipboard::Clipboard);
258 (QApplication::clipboard())->
setText(txt, QClipboard::Selection);
267 QString tmp_str = (QApplication::clipboard())->
text(bClipboard ? QClipboard::Clipboard : QClipboard::Selection);
269 if (tmp_str.isNull()) {
271 KNotification::beep();
276 NumBase tmp_num_base = num_base_;
279 tmp_str = tmp_str.trimmed();
282 tmp_str.remove(KGlobal::locale()->thousandsSeparator());
285 tmp_str = tmp_str.toLower();
288 if (tmp_str.startsWith(QLatin1String(
"0x"))) {
290 tmp_str.remove(0, 2);
291 }
else if (tmp_str.startsWith(QLatin1String(
"0b"))) {
293 tmp_str.remove(0, 2);
294 }
else if (tmp_str.startsWith(QLatin1String(
"0"))) {
298 tmp_str.remove(0, 1);
304 const qint64 tmp_result = tmp_str.toULongLong(&was_ok, tmp_num_base);
309 KNotification::beep();
317 KNotification::beep();
326 void KCalcDisplay::slotDisplaySelected() {
328 if (button_ == Qt::LeftButton) {
331 selection_timer_->start(100);
333 selection_timer_->stop();
346 void KCalcDisplay::slotSelectionTimedOut() {
350 selection_timer_->stop();
357 void KCalcDisplay::invertColors() {
359 QPalette tmp_palette = palette();
360 tmp_palette.setColor(QPalette::Base, palette().color(QPalette::Text));
361 tmp_palette.setColor(QPalette::Text, palette().color(QPalette::Base));
362 setPalette(tmp_palette);
371 if (e->button() == Qt::LeftButton) {
373 button_ = Qt::LeftButton;
375 button_ = Qt::MidButton;
387 precision_ = precision;
396 if (fixed_precision_ > precision_) {
397 fixed_precision_ = -1;
399 fixed_precision_ = precision;
424 twoscomplement_ = flag;
432 binaryGrouping_ = digits;
440 octalGrouping_ = digits;
448 hexadecimalGrouping_ = digits;
456 return display_amount_;
467 str_int_ = QLatin1String(
"0");
468 str_int_exp_.clear();
476 if (twoscomplement_) {
478 const quint64 tmp_workaround = display_amount_.
toUint64();
479 display_str = QString::number(tmp_workaround, num_base_).toUpper();
482 qint64 tmp_workaround = display_amount_.
toInt64();
483 const bool neg = tmp_workaround < 0;
485 tmp_workaround = qAbs(tmp_workaround);
488 display_str = QString::number(tmp_workaround, num_base_).toUpper();
490 display_str.prepend(KGlobal::locale()->negativeSign());
495 display_amount_ = new_amount;
514 const bool special = (
string.contains(QLatin1String(
"nan")) ||
string.contains(QLatin1String(
"inf")));
519 if (groupdigits_ && !special){
522 if (
string.endsWith(QLatin1Char(
'.'))) {
525 text_ = KGlobal::locale()->formatNumber(text_,
false, 0);
526 text_.append(KGlobal::locale()->decimalSymbol());
529 text_ = KGlobal::locale()->formatNumber(text_,
false, 0);
549 if(
string.contains(QLatin1String(
"inf"))) {
550 text_.replace(
"inf", QChar(0x221e));
565 QString tmpDisplayString;
566 const int stringLength = displayString.length();
568 for (
int i = stringLength; i > 0 ; i--){
569 if(i % numDigits == 0 && i != stringLength) {
570 tmpDisplayString = tmpDisplayString +
' ';
573 tmpDisplayString = tmpDisplayString + displayString[stringLength - i];
576 return tmpDisplayString;
628 str_status_[i] =
text;
643 tmp_string = QLatin1Char(
'-') + str_int_;
645 tmp_string = str_int_;
652 Q_ASSERT(!period_ && !eestate_);
654 display_amount_ =
KNumber(str_int_.toULongLong(&ok, 2));
656 display_amount_ = -display_amount_;
661 Q_ASSERT(!period_ && !eestate_);
663 display_amount_ =
KNumber(str_int_.toULongLong(&ok, 8));
665 display_amount_ = -display_amount_;
670 Q_ASSERT(!period_ && !eestate_);
672 display_amount_ =
KNumber(str_int_.toULongLong(&ok, 16));
674 display_amount_ = -display_amount_;
681 display_amount_ =
KNumber(tmp_string);
683 if (str_int_exp_.isNull()) {
685 display_amount_ =
KNumber(tmp_string);
686 setText(tmp_string + QLatin1String(
"e0"));
688 tmp_string += QLatin1Char(
'e') + str_int_exp_;
690 display_amount_ =
KNumber(tmp_string);
709 switch (new_char.toLatin1()) {
714 KNotification::beep();
729 KNotification::beep();
738 KNotification::beep();
751 KNotification::beep();
761 if(new_char == KGlobal::locale()->decimalSymbol()[0]) {
764 if (num_base_ !=
NB_DECIMAL || period_ || eestate_) {
766 KNotification::beep();
773 KNotification::beep();
782 if (new_char == QLatin1Char(
'e') && str_int_.endsWith(KGlobal::locale()->decimalSymbol())) {
789 if (new_char != QLatin1Char(
'e') && !(str_int_exp_.isNull() && new_char == QLatin1Char(
'0'))) {
790 str_int_exp_.append(new_char);
794 if (str_int_ == QLatin1String(
"0")) {
795 switch (new_char.toLatin1()) {
799 str_int_.append(new_char);
802 if(new_char == KGlobal::locale()->decimalSymbol()[0]) {
804 str_int_.append(new_char);
807 str_int_[0] = new_char;
811 str_int_.append(new_char);
826 if (str_int_exp_.isNull()) {
829 const int length = str_int_exp_.length();
831 str_int_exp_.chop(1);
833 str_int_exp_ = QLatin1String((
const char *)0);
837 const int length = str_int_.length();
839 if (str_int_[length-1] == KGlobal::locale()->decimalSymbol()[0]) {
845 str_int_[0] = QLatin1Char(
'0');
858 bool KCalcDisplay::changeSign() {
861 if (str_int_ == QLatin1String(
"0")) {
866 if (!str_int_exp_.isNull()) {
867 if (str_int_exp_[0] != QLatin1Char(
'-')) {
868 str_int_exp_.prepend(QLatin1Char(
'-'));
870 str_int_exp_.remove(QLatin1Char(
'-'));
874 neg_sign_ = !neg_sign_;
885 void KCalcDisplay::initStyleOption(QStyleOptionFrame *option)
const {
891 option->initFrom(
this);
892 option->state &= ~QStyle::State_HasFocus;
894 if (frameShadow() == QFrame::Sunken) {
895 option->state |= QStyle::State_Sunken;
896 }
else if (frameShadow() == QFrame::Raised) {
897 option->state |= QStyle::State_Raised;
900 option->lineWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, option,
this);
901 option->midLineWidth = 0;
910 QPainter painter(
this);
912 QStyleOptionFrame option;
913 initStyleOption(&option);
915 style()->drawPrimitive(QStyle::PE_PanelLineEdit, &option, &painter,
this);
918 const int margin = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, 0, 0);
919 QRect cr = contentsRect();
920 cr.adjust(margin*2, 0, -margin*2, 0);
922 const int align = QStyle::visualAlignment(layoutDirection(), Qt::AlignRight | Qt::AlignVCenter);
923 painter.drawText(cr, align | Qt::TextSingleLine, text_);
928 fnt.setPointSize(qMax((fnt.pointSize() / 2), 7));
929 painter.setFont(fnt);
931 QFontMetrics fm(fnt);
932 const uint w = fm.width(QLatin1String(
"________"));
933 const uint h = fm.height();
936 painter.drawText(5 + n * w, h, str_status_[n]);
947 QSize sz = fontMetrics().size(Qt::TextSingleLine, text_);
951 fnt.setPointSize(qMax((fnt.pointSize() / 2), 7));
953 const QFontMetrics fm(fnt);
954 sz.setHeight(sz.height() + fm.height());
956 QStyleOptionFrame option;
957 initStyleOption(&option);
959 return (style()->sizeFromContents(QStyle::CT_LineEdit, &option, sz.expandedTo(QApplication::globalStrut()),
this));
void setGroupDigits(bool flag)
void setBinaryGrouping(int digits)
static uint fixedPrecision()
Get Number of fixed decimal digits.
QString toQString(int width=-1, int precision=-1) const
static bool groupDigits()
Get Whether to group digits.
void enterDigit(int data)
static QColor backColor()
Get The background color of the display.
static uint octalGrouping()
Get Octal figures will be separated by a whitespace after every Xth digit.
static QString decimalSeparator()
static void setDefaultFloatOutput(bool x)
static const KNumber Zero
const KNumber & getAmount() const
bool sendEvent(Event event)
void setHexadecimalGrouping(int digits)
static QFont displayFont()
Get The font to use in the display.
void slotPaste(bool bClipboard=true)
KNumber integerPart() const
static bool beep()
Get Whether to beep on error.
static uint binaryGrouping()
Get Binary figures will be separated by a whitespace after every Xth digit.
void setStatusText(int i, const QString &text)
void changedText(const QString &)
void setTwosComplement(bool flag)
bool setAmount(const KNumber &new_amount)
void setPrecision(int precision)
virtual QSize sizeHint() const
static void setDefaultFractionalInput(bool x)
void changedAmount(const KNumber &)
void setFixedPrecision(int precision)
KNumber lastOutput(bool &error) const
void setOctalGrouping(int digits)
static uint hexadecimalGrouping()
Get Hexadecimal figures will be separated by a whitespace after every Xth digit.
void setText(const QString &string)
int setBase(NumBase new_base)
static uint precision()
Get Maximum number of digits displayed.
KCalcDisplay(QWidget *parent=0)
static QColor foreColor()
Get The foreground color of the display.
void newCharacter(const QChar new_char)
static bool twosComplement()
Get Whether to use Two's Complement for non-decimal numbers.
QString groupDigits(const QString &displayString, int numDigits)
void updateFromCore(const CalcEngine &core, bool store_result_in_history=false)
static bool fixed()
Get Whether to use fixed decimal places.
void mousePressEvent(QMouseEvent *)
virtual void paintEvent(QPaintEvent *p)