⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tewidget.cpp

📁 Trolltech公司发布的图形界面操作系统。可在qt-embedded-2.3.10平台上编译为嵌入式图形界面操作系统。
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    if (!resizing) // not while resizing, we're expecting a paintEvent    for (x = 0; x < cols; x++)    {      hasBlinker |= (ext[x].r & RE_BLINK);      if (ext[x] != lcl[x])      {        cr = ext[x].r;        cb = ext[x].b;        if (ext[x].f != cf) cf = ext[x].f;        int lln = cols - x;        disstrU[0] = fontMap(ext[x+0].c);        for (len = 1; len < lln; len++)        {          if (ext[x+len].f != cf || ext[x+len].b != cb || ext[x+len].r != cr ||              ext[x+len] == lcl[x+len] )            break;          disstrU[len] = fontMap(ext[x+len].c);        }        QString unistr(disstrU,len);        drawAttrStr(paint,                    QRect(blX+tLx+font_w*x,bY+tLy+font_h*y,font_w*len,font_h),                    unistr, ext[x], pm != NULL, true);        x += len - 1;      }    }    // finally, make `image' become `newimg'.    memcpy((void*)lcl,(const void*)ext,cols*sizeof(ca));  }  drawFrame( &paint );  paint.end();  setUpdatesEnabled(TRUE);  if ( hasBlinker && !blinkT->isActive()) blinkT->start(1000); // 1000 ms  if (!hasBlinker &&  blinkT->isActive()) { blinkT->stop(); blinking = FALSE; }  delete [] disstrU;}// paint Event /////////////////////////////////////////////////////*    The difference of this routine vs. the `setImage' is,    that the drawing does not include a difference analysis    between the old and the new image. Instead, the internal    image is used and the painting bound by the PaintEvent box.*/void TEWidget::paintEvent( QPaintEvent* pe ){//{ static int cnt = 0; printf("paint %d\n",cnt++); }  const QPixmap* pm = backgroundPixmap();  QPainter paint;  setUpdatesEnabled(FALSE);  paint.begin( this );  paint.setBackgroundMode( TransparentMode );HCNT("paintEvent");  // Note that the actual widget size can be slightly larger  // than the image (the size is truncated towards the smaller  // number of characters in `resizeEvent'. The paint rectangle  // can thus be larger than the image, but less then the size  // of one character.  QRect rect = pe->rect().intersect(contentsRect());  QPoint tL  = contentsRect().topLeft();  int    tLx = tL.x();  int    tLy = tL.y();  int lux = QMIN(columns-1, QMAX(0,(rect.left()   - tLx - blX ) / font_w));  int luy = QMIN(lines-1,   QMAX(0,(rect.top()    - tLy - bY  ) / font_h));  int rlx = QMIN(columns-1, QMAX(0,(rect.right()  - tLx - blX ) / font_w));  int rly = QMIN(lines-1,   QMAX(0,(rect.bottom() - tLy - bY  ) / font_h));  /* printf("paintEvent: %d..%d, %d..%d (%d..%d, %d..%d)\n",lux,rlx,luy,rly,  rect.left(), rect.right(), rect.top(), rect.bottom());  */  //  if (pm != NULL && color_table[image->b].transparent)  //  erase(rect);  // BL: I have no idea why we need this, and it breaks the refresh.  QChar *disstrU = new QChar[columns];  for (int y = luy; y <= rly; y++)  for (int x = lux; x <= rlx; x++)  {    int len = 1;    disstrU[0] = fontMap(image[loc(x,y)].c);    int cf = image[loc(x,y)].f;    int cb = image[loc(x,y)].b;    int cr = image[loc(x,y)].r;    while (x+len <= rlx &&           image[loc(x+len,y)].f == cf &&           image[loc(x+len,y)].b == cb &&           image[loc(x+len,y)].r == cr )    {      disstrU[len] = fontMap(image[loc(x+len,y)].c);      len += 1;    }    QString unistr(disstrU,len);    drawAttrStr(paint,                QRect(blX+tLx+font_w*x,bY+tLy+font_h*y,font_w*len,font_h),                unistr, image[loc(x,y)], pm != NULL, false);    x += len - 1;  }  delete [] disstrU;  drawFrame( &paint );  paint.end();  setUpdatesEnabled(TRUE);}void TEWidget::blinkEvent(){  blinking = !blinking;  repaint(FALSE);}/* ------------------------------------------------------------------------- *//*                                                                           *//*                                  Resizing                                 *//*                                                                           *//* ------------------------------------------------------------------------- */void TEWidget::resizeEvent(QResizeEvent* ev){  //printf("resize: %d,%d\n",ev->size().width(),ev->size().height());  //printf("approx: %d,%d\n",ev->size().width()/font_w,ev->size().height()/font_h);  //printf("leaves: %d,%d\n",ev->size().width()%font_w,ev->size().height()%font_h);  //printf("curren: %d,%d\n",width(),height());HCNT("resizeEvent");  // see comment in `paintEvent' concerning the rounding.  //FIXME: could make a routine here; check width(),height()  assert(ev->size().width() == width());  assert(ev->size().height() == height());  propagateSize();}void TEWidget::propagateSize(){  ca* oldimg = image;  int oldlin = lines;  int oldcol = columns;  makeImage();  // we copy the old image to reduce flicker  int lins = QMIN(oldlin,lines);  int cols = QMIN(oldcol,columns);  if (oldimg)  {    for (int lin = 0; lin < lins; lin++)      memcpy((void*)&image[columns*lin],             (void*)&oldimg[oldcol*lin],cols*sizeof(ca));    free(oldimg); //FIXME: try new,delete  }  else    clearImage();  //NOTE: control flows from the back through the chest right into the eye.  //      `emu' will call back via `setImage'.  resizing = TRUE;  emit changedImageSizeSignal(lines, columns); // expose resizeEvent  resizing = FALSE;}/* ------------------------------------------------------------------------- *//*                                                                           *//*                                Scrollbar                                  *//*                                                                           *//* ------------------------------------------------------------------------- */void TEWidget::scrollChanged(int){  emit changedHistoryCursor(scrollbar->value()); //expose}void TEWidget::setScroll(int cursor, int slines){  disconnect(scrollbar, SIGNAL(valueChanged(int)), this, SLOT(scrollChanged(int)));  scrollbar->setRange(0,slines);  scrollbar->setSteps(1,lines);  scrollbar->setValue(cursor);  connect(scrollbar, SIGNAL(valueChanged(int)), this, SLOT(scrollChanged(int)));}void TEWidget::setScrollbarLocation(int loc){  if (scrollLoc == loc) return; // quickly  scrollLoc = loc;  propagateSize();  update();}/* ------------------------------------------------------------------------- *//*                                                                           *//*                                   Mouse                                   *//*                                                                           *//* ------------------------------------------------------------------------- *//*?    Three different operations can be performed using the mouse and the    routines in this section serve all of them:    1) The press/release events are exposed to the application    2) Marking (press and move left button) and Pasting (press middle button)    3) The right mouse button is used from the configuration menu    NOTE: During the marking process we attempt to keep the cursor within    the bounds of the text as being displayed by setting the mouse position    whenever the mouse has left the text area.    Two reasons to do so:    1) QT does not allow the `grabMouse' to confine-to the TEWidget.       Thus a `XGrapPointer' would have to be used instead.    2) Even if so, this would not help too much, since the text area       of the TEWidget is normally not identical with it's bounds.    The disadvantage of the current handling is, that the mouse can visibly    leave the bounds of the widget and is then moved back. Because of the    current construction, and the reasons mentioned above, we cannot do better    without changing the overall construction.*//**/void TEWidget::mousePressEvent(QMouseEvent* ev){//printf("press [%d,%d] %d\n",ev->x()/font_w,ev->y()/font_h,ev->button());  if ( !contentsRect().contains(ev->pos()) ) return;  QPoint tL  = contentsRect().topLeft();  int    tLx = tL.x();  int    tLy = tL.y();  word_selection_mode = FALSE;//printf("press top left [%d,%d] by=%d\n",tLx,tLy, bY);  if ( ev->button() == LeftButton)  {    QPoint pos = QPoint((ev->x()-tLx-blX)/font_w,(ev->y()-tLy-bY)/font_h);    if ( ev->state() & ControlButton ) preserve_line_breaks = FALSE ;    if (mouse_marks || (ev->state() & ShiftButton))    {      emit clearSelectionSignal();      iPntSel = pntSel = pos;      actSel = 1; // left mouse button pressed but nothing selected yet.      grabMouse(   /*crossCursor*/  ); // handle with care!     }    else    {      emit mouseSignal( 0, pos.x() + 1, pos.y() + 1 ); // left button    }  }  if ( ev->button() == MidButton )  {    emitSelection();  }  if ( ev->button() == RightButton ) // Configure  {    emit configureRequest( this, ev->state()&(ShiftButton|ControlButton), ev->x(), ev->y() );  }}void TEWidget::mouseMoveEvent(QMouseEvent* ev){  // for auto-hiding the cursor, we need mouseTracking  if (ev->state() == NoButton ) return;  if (actSel == 0) return; // don't extend selection while pasting  if (ev->state() & MidButton) return;  //if ( !contentsRect().contains(ev->pos()) ) return;  QPoint tL  = contentsRect().topLeft();  int    tLx = tL.x();  int    tLy = tL.y();  int    scroll = scrollbar->value();  // we're in the process of moving the mouse with the left button pressed  // the mouse cursor will remain caught within the bounds of the text in  // this widget.  // Adjust position within text area bounds. See FIXME above.  QPoint pos = ev->pos();  if ( pos.x() < tLx+blX )                  pos.setX( tLx+blX );  if ( pos.x() > tLx+blX+columns*font_w-1 ) pos.setX( tLx+blX+columns*font_w );  if ( pos.y() < tLy+bY )                   pos.setY( tLy+bY );  if ( pos.y() > tLy+bY+lines*font_h-1 )    pos.setY( tLy+bY+lines*font_h-1 );  // check if we produce a mouse move event by this  if ( pos != ev->pos() ) cursor().setPos(mapToGlobal(pos));  if ( pos.y() == tLy+bY+lines*font_h-1 )  {    scrollbar->setValue(scrollbar->value()+yMouseScroll); // scrollforward  }  if ( pos.y() == tLy+bY )  {    scrollbar->setValue(scrollbar->value()-yMouseScroll); // scrollback  }  QPoint here = QPoint((pos.x()-tLx-blX)/font_w,(pos.y()-tLy-bY)/font_h);  QPoint ohere;  bool swapping = FALSE;  if ( word_selection_mode )  {    // Extend to word boundaries    int i;    int selClass;    bool left_not_right = ( here.y() < iPntSel.y() ||     here.y() == iPntSel.y() && here.x() < iPntSel.x() );    bool old_left_not_right = ( pntSel.y() < iPntSel.y() ||     pntSel.y() == iPntSel.y() && pntSel.x() < iPntSel.x() );    swapping = left_not_right != old_left_not_right;    // Find left (left_not_right ? from here : from start)    QPoint left = left_not_right ? here : iPntSel;    i = loc(left.x(),left.y());    selClass = charClass(image[i].c);    while ( left.x() > 0 && charClass(image[i-1].c) == selClass )    { i--; left.rx()--; }    // Find left (left_not_right ? from start : from here)    QPoint right = left_not_right ? iPntSel : here;    i = loc(right.x(),right.y());    selClass = charClass(image[i].c);    while ( right.x() < columns-1 && charClass(image[i+1].c) == selClass )    { i++; right.rx()++; }    // Pick which is start (ohere) and which is extension (here)    if ( left_not_right )    {      here = left; ohere = right;    }    else    {      here = right; ohere = left;    }  }  if (here == pntSel && scroll == scrollbar->value()) return; // not moved  if ( word_selection_mode ) {    if ( actSel < 2 || swapping ) {      emit beginSelectionSignal( ohere.x(), ohere.y() );    }  } else if ( actSel < 2 ) {    emit beginSelectionSignal( pntSel.x(), pntSel.y() );  }  actSel = 2; // within selection  pntSel = here;  emit extendSelectionSignal( here.x(), here.y() );}void TEWidget::mouseReleaseEvent(QMouseEvent* ev){//printf("release [%d,%d] %d\n",ev->x()/font_w,ev->y()/font_h,ev->button());  if ( ev->button() == LeftButton)  {    if ( actSel > 1 ) emit endSelectionSignal(preserve_line_breaks);    preserve_line_breaks = TRUE;    actSel = 0;    //FIXME: emits a release event even if the mouse is    //       outside the range. The procedure used in `mouseMoveEvent'    //       applies here, too.    QPoint tL  = contentsRect().topLeft();    int    tLx = tL.x();    int    tLy = tL.y();    if (!mouse_marks && !(ev->state() & ShiftButton))      emit mouseSignal( 3, // release                        (ev->x()-tLx-blX)/font_w + 1,                        (ev->y()-tLy-bY)/font_h + 1 );    releaseMouse();  }}void TEWidget::mouseDoubleClickEvent(QMouseEvent* ev){  if ( ev->button() != LeftButton) return;  QPoint tL  = contentsRect().topLeft();  int    tLx = tL.x();  int    tLy = tL.y();  QPoint pos = QPoint((ev->x()-tLx-blX)/font_w,(ev->y()-tLy-bY)/font_h);  // pass on double click as two clicks.  if (!mouse_marks && !(ev->state() & ShiftButton))  {    emit mouseSignal( 0, pos.x()+1, pos.y()+1 ); // left button    emit mouseSignal( 3, pos.x()+1, pos.y()+1 ); // release    emit mouseSignal( 0, pos.x()+1, pos.y()+1 ); // left button    return;  }  emit clearSelectionSignal();  QPoint bgnSel = pos;  QPoint endSel = QPoint((ev->x()-tLx-blX)/font_w,(ev->y()-tLy-bY)/font_h);  int i = loc(bgnSel.x(),bgnSel.y());  iPntSel = bgnSel;  word_selection_mode = TRUE;  // find word boundaries...  int selClass = charClass(image[i].c);  {    // set the start...     int x = bgnSel.x();     while ( x > 0 && charClass(image[i-1].c) == selClass )     { i--; x--; }     bgnSel.setX(x);     emit beginSelectionSignal( bgnSel.x(), bgnSel.y() );     // set the end...     i = loc( endSel.x(), endSel.y() );     x = endSel.x();     while( x < columns-1 && charClass(image[i+1].c) == selClass )     { i++; x++ ; }     endSel.setX(x);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -