summaryrefslogtreecommitdiffstats
path: root/kcalc/kcalc_const_button.cpp
blob: a8eb9891a13ead35e0c2802a53aa846063f04425 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
    kCalculator, a simple scientific calculator for KDE

    Copyright (C) 2003 Klaus Niederkrueger <kniederk@math.uni-koeln.de>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/

#include <tqstring.h>


#include <kcmenumngr.h>
#include <kinputdialog.h>
#include <kpopupmenu.h>

#include "kcalc_const_button.h"
#include "kcalc_const_menu.h"
#include "kcalc_settings.h"


KCalcConstButton::KCalcConstButton(TQWidget *tqparent, int but_num, const char * name)
  : KCalcButton(tqparent, name), _button_num(but_num)
{
  addMode(ModeInverse, "Store", i18n("Write display data into memory"));
  
  initPopupMenu();
}


KCalcConstButton::KCalcConstButton(const TQString &label, TQWidget *tqparent, int but_num,
                                   const char * name, const TQString &tooltip)
  : KCalcButton(label, tqparent, name, tooltip), _button_num(but_num)
{
  addMode(ModeInverse, "Store", i18n("Write display data into memory"));
  
  initPopupMenu();
}

TQString KCalcConstButton::constant(void) const
{
  return KCalcSettings::valueConstant(_button_num);
}

void KCalcConstButton::setLabelAndTooltip(void)
{
  TQString new_label = TQString("C") + TQString().setNum(_button_num + 1);
  TQString new_tooltip;
  
  new_label = (KCalcSettings::nameConstant(_button_num).isNull() ? new_label : KCalcSettings::nameConstant(_button_num));
  
  new_tooltip = new_label + "=" + KCalcSettings::valueConstant(_button_num);
  
  addMode(ModeNormal, new_label, new_tooltip);
}

void KCalcConstButton::initPopupMenu(void)
{
  KCalcConstMenu *tmp_menu = new KCalcConstMenu(this);
  
  _popup = new KPopupMenu(this, "set const-cutton");
  _popup->insertItem(i18n("Set Name"), 0);
  _popup->insertItem(i18n("Choose From List"), tmp_menu, 1);
  
  connect(_popup, TQT_SIGNAL(activated(int)), TQT_SLOT(slotConfigureButton(int)));
  connect(tmp_menu, TQT_SIGNAL(activated(int)), TQT_SLOT(slotChooseScientificConst(int)));

  KContextMenuManager::insert(this, _popup);
}

void KCalcConstButton::slotConfigureButton(int option)
{
  if (option == 0)
    {
      bool yes_no;
      TQString input = KInputDialog::text(i18n("New Name for Constant"), i18n("New name:"),
					 text(), &yes_no, this, "nameUserConstants-Dialog");
      if(yes_no) {
	KCalcSettings::setNameConstant(_button_num, input);
	setLabelAndTooltip();
      }
    }
}

void KCalcConstButton::slotChooseScientificConst(int option)
{
  KCalcSettings::setValueConstant(_button_num,
				  KCalcConstMenu::Constants[option].value);

  KCalcSettings::setNameConstant(_button_num,
				 KCalcConstMenu::Constants[option].label);
  
  setLabelAndTooltip();
}

#include "kcalc_const_button.moc"