/**********************************************************************/
/*   TimeMon (c)  1994  Helmut Maierhofer			      */
/*   KDE-ified M. Maierhofer 1998                                     */
/**********************************************************************/

/*
 * confdlg.h
 *
 * Definitions for the timemon configuration dialog.
 */

#include <config.h>
#include <stdio.h>
#include <qgroupbox.h>
#include <qlineedit.h>
#include <qslider.h>
#include <qlayout.h>
#include <qlabel.h>
#include <qvgroupbox.h>

#include <klocale.h>
#include <knuminput.h>
#include <kmessagebox.h>

#include "confdlg.h"
#include "timemon.h"

#include "confdlg.moc"

// -- KConfDialog definition ---------------------------------------------

KConfDialog::KConfDialog(KTimeMon *t)
    : timemon(t), haveWarned(false)
{
    QWidget *w;
    QBoxLayout *bl;
    QGridLayout *gl;
    QLabel *l;
    QGroupBox *b;
    KColorButton *cb;
    unsigned i, j;

    setCaption(i18n("KTimeMon Configuration"));

    // first tab: general
    w = new QWidget(this);

    bl = new QVBoxLayout(w, 5);

    b = new QVGroupBox(i18n("Sample &Rate:"), w);
    bl->addWidget(b);

    intervalEdit = new KIntNumInput(250, b);
    intervalEdit->setRange(20, 1000, 10);
    intervalEdit->setSuffix(i18n("ms"));

    // scaling group box
    b = new QVGroupBox(i18n("Scaling"), w);
    bl->addWidget(b, 5);

    autoScaleBox = new QCheckBox(i18n("&Automatic"), b);
    connect(autoScaleBox, SIGNAL(toggled(bool)), this, SLOT(toggle(bool)));

    pageScaleEdit = new KIntNumInput(intervalEdit, 1000, b);
    pageScaleEdit->setRange(10, 10000, 10);
    pageScaleEdit->setLabel(i18n("&Paging:"), AlignVCenter | AlignLeft);

    swapScaleEdit = new KIntNumInput(pageScaleEdit, 1000, b);
    swapScaleEdit->setRange(1, 10000, 5);
    swapScaleEdit->setLabel(i18n("&Swapping:"), AlignVCenter | AlignLeft);

    ctxScaleEdit  = new KIntNumInput(swapScaleEdit, 10000, b);
    ctxScaleEdit->setLabel(i18n("&Context Switch:"), AlignVCenter | AlignLeft);
    ctxScaleEdit->setRange(1, 10000, 30);

    bl->addStretch(1);

    addTab(w, i18n("&General"));


    // second tab: colours
    w = new QWidget(this);

    gl = new QGridLayout(w, 12, 10, 5);

    gl->setColStretch(3, 1); gl->setColStretch(6, 1); // eat up horizontal space
    gl->setRowStretch(11, 1);	// eat up vertical space

    gl->addRowSpacing(0, 20); gl->addRowSpacing(4, 20); gl->addRowSpacing(8, 20);
    gl->addRowSpacing(2, 8); gl->addRowSpacing(6, 8); gl->addRowSpacing(10, 8);

    gl->addColSpacing(0, 10); gl->addColSpacing(9, 25);

    QString colourLabels[2][3];
    colourLabels[0][0] = i18n("Kernel");
    colourLabels[0][1] = i18n("User");
#if defined(__sun__)
    colourLabels[0][2] = i18n("Wait");
#else
    colourLabels[0][2] = i18n("Nice");
#endif

    colourLabels[1][0] = i18n("Used");
    colourLabels[1][1] = i18n("Buffers");
#if defined(__sun__) || defined(__osf__)
    colourLabels[1][2] = i18n("Kernel");
#else
    colourLabels[1][2] = i18n("Cached");
#endif
#
    KColorButton **colourButtons[2][3] = { { &kernelCB, &userCB, &niceCB },
                                           { &usedCB, &buffersCB, &cachedCB }};
    for (i = 0; i < 2; i++) {
        b = new QGroupBox((i ? i18n("Memory/Swapping") : i18n("CPU/Paging")), w);
        gl->addMultiCellWidget(b, 4*i, 4*i+2, 0, 9);

        for (j = 0; j < 3; j++) {
            l = new QLabel(colourLabels[i][j], w);
            gl->addWidget(l, 4*i+1, 3*j+1, AlignVCenter | AlignRight);

            cb = *colourButtons[i][j] = new KColorButton(white, w);
            gl->addWidget(cb, 4*i+1, 3*j+2, AlignCenter);

            connect(cb, SIGNAL(changed(const QColor &)),
                    this, SLOT(updateSampleWidget(const QColor &)));
        }
    }

    b = new QGroupBox(i18n("Swap/Background"), w);
    gl->addMultiCellWidget(b, 8, 10, 0, 6);

    l = new QLabel(i18n("Swap"), w);
    gl->addWidget(l, 9, 1, AlignVCenter | AlignRight);

    cb = swapCB = new KColorButton(red, w);
    gl->addWidget(cb, 9, 2);

    connect(cb, SIGNAL(changed(const QColor &)),
            this, SLOT(updateSampleWidget(const QColor &)));

    l = new QLabel(i18n("Backgd"), w);
    gl->addWidget(l, 9, 4, AlignVCenter | AlignRight);

    cb = bgCB = new KColorButton(blue, w);
    gl->addWidget(cb, 9, 5);

  connect(cb, SIGNAL(changed(const QColor &)),
	  this, SLOT(updateSampleWidget(const QColor &)));

  b = new QGroupBox(i18n("Sample"), w);
  gl->addMultiCellWidget(b, 8, 10, 7, 9);

//  sample = new KTimeMonWidget(0, false, true, b);
//  sample->setGeometry(38, 25, 35, 35);

  addTab(w, i18n("&Colors"));


  // third tab: interaction
  w = new QWidget(this);
  addTab(w, i18n("&Interaction"));

  bl = new QVBoxLayout(w, 5);

  b = new QGroupBox(i18n("Mouse Events"), w);
  bl->addWidget(b, 5);

  gl = new QGridLayout(b, MAX_MOUSE_ACTIONS + 1, 3, 10);

  for (i = 1; i < MAX_MOUSE_ACTIONS + 1; i++)
    gl->setRowStretch(i, 1);
  gl->setColStretch(2, 1);

  QString buttonText[MAX_MOUSE_ACTIONS] = { i18n("Left button"),
						i18n("Middle button"),
						i18n("Right button") };
  for (i = 0; i < (int) MAX_MOUSE_ACTIONS; i++) {

    l = new QLabel(i18n(buttonText[i]), b);
    gl->addWidget(l, i+1, 0);

    mouseC[i] = new QComboBox(false, b);
    mouseC[i]->insertItem(i18n("is ignored"), KTimeMon::NOTHING);
    mouseC[i]->insertItem(i18n("switches mode"), KTimeMon::SWITCH);
    mouseC[i]->insertItem(i18n("pops up menu"), KTimeMon::MENU);
    mouseC[i]->insertItem(i18n("starts"), KTimeMon::COMMAND);
    mouseC[i]->setCurrentItem(t->mouseAction[i]);
    gl->addWidget(mouseC[i], i+1, 1);

    connect(mouseC[i], SIGNAL(activated(int)), this,
	    SLOT(mouseInteraction(int)));

    mouseLE[i] = new KLineEdit(b);
    mouseLE[i]->setText(t->mouseActionCommand[i]);
    gl->addWidget(mouseLE[i], i+1, 2);
  }

  gl->activate();

  mouseInteraction(0);		// update enable/disable etc

  b = new QVGroupBox(i18n("Tool Tips"), w);
  bl->addWidget(b, 5);

  toolTipBox = new QCheckBox(i18n("Tool Tips enabled"), b);

  bl->addStretch(1);

  setApplyButton();
  setCancelButton();
  resize(380, 300);

  connect(this, SIGNAL(applyButtonPressed()), timemon, SLOT(apply()));
}


// Adjust the colours of the sample widget in the configuration dialog.
void KConfDialog::updateSampleWidget(const QColor &)
{
#if 0
  sample->kernelColour = kernelCB->color();
  sample->userColour = userCB->color();
  sample->niceColour = niceCB->color();
  sample->cachedColour = cachedCB->color();
  sample->usedColour = usedCB->color();
  sample->buffersColour = buffersCB->color();
  sample->swapColour = swapCB->color();
  sample->bgColour = bgCB->color();
  sample->update();
#endif
}

// -----------------------------------------------------------------------------

// enable/disable the scale widgets
void KConfDialog::toggle(bool state)
{
    swapScaleEdit->setEnabled(!state);
    pageScaleEdit->setEnabled(!state);
    ctxScaleEdit->setEnabled(!state);
}


// -----------------------------------------------------------------------------

// Check for valid settings.
void KConfDialog::mouseInteraction(int)
{
  int i;
  bool menuOk = false;

  for (i = 0; i < MAX_MOUSE_ACTIONS; i++) {
    unsigned action = mouseC[i]->currentItem();

    if (action == KTimeMon::MENU) menuOk = true;
    mouseLE[i]->setEnabled(action == KTimeMon::COMMAND);
  }

  if (!menuOk && !haveWarned) {
    KMessageBox::information(timemon, i18n("KTimeMon notice"),
			 i18n("You have set the configuration so that\n"
			      "the pop-up menu cannot be invoked by a\n"
			      "mouse click. This is probably not such a\n"
			      "good idea, because you will have to edit\n"
			      "the configuration options manually to get\n"
			      "rid of this setting..."));
    haveWarned = true;
  }

}


// -----------------------------------------------------------------------------

// update the dialog fields
void KConfDialog::update()
{
  intervalEdit->setValue(timemon->interval);
  kernelCB->setColor(timemon->kernelColour);
  userCB->setColor(timemon->userColour);
  niceCB->setColor(timemon->niceColour);
  buffersCB->setColor(timemon->buffersColour);
  usedCB->setColor(timemon->usedColour);
  cachedCB->setColor(timemon->cachedColour);
  swapCB->setColor(timemon->swapColour);
  bgCB->setColor(timemon->bgColour);
  pageScaleEdit->setValue(timemon->pageScale);
  swapScaleEdit->setValue(timemon->swapScale);
  ctxScaleEdit->setValue(timemon->ctxScale);
  autoScaleBox->setChecked(timemon->autoScale);
//  toggle(timemon->autoScale);
  toolTipBox->setChecked(timemon->tooltip);
  updateSampleWidget(white);	// fake colour
}
