Example #1
0
bool IPWidget::keyEvent(KeyEvent &ke)
{
    // So we accept up/down when we are over a number field
    //XXXX:NNN.NNN.NNN.NNN
    // Is it even in our space

    Dout(dc::trace,"IPWidget ::keyEvent at " << ke.x() << " " << ke.y());

    if (!contains(ke.x(),ke.y())) return false;
    setFocusWidget(true);

    int ev=ke.event();
    if ((ev & KeyEvent::KeyRelease))
    {
        if ((ev & KeyEvent::KeyUp) || (ev & KeyEvent::KeyDown))
        {
            int col=ke.x()-x();
            int row=ke.y()-y();
            if ((col != 3) && (col!= 7) && (col!= 11)) // tweak a digit
            {
                int sgn=-1;
                if (ev & KeyEvent::KeyUp) sgn=1;
                int pos = col;
                if (ipv==IPV6 && row ==1)
                    pos += 16;
                int pwr = 2 - (pos % 4);
                int incr = sgn;
                for (int i=0; i<pwr; i++)
                    incr*=10;
                std::string quad=ip.substr((pos/4)*4,3);
                std::istringstream iss(quad);
                int iquad;
                iss>>iquad;
                fprintf(stderr,"%i %s %i\n",incr,quad.c_str(),iquad);
                iquad += incr;
                // Range is 0..255 for a quad
                // What we do is first get the current quad value, the amount we are
                // incrementing by and cycle through, clamping to valid values
                if (iquad <0 || iquad> 255) iquad-=incr;
                std::ostringstream oss;
                oss << std::setfill('0') << std::setw(3) << iquad;

                ip.replace((pos/4)*4,3,oss.str());
                fprintf(stderr,"%i %s %s\n",iquad,oss.str().c_str(),ip.c_str());
                setDirty(true);
                return true;
            }
        }