Back to index

texmacs  1.0.7.15
qt_view_widget.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003  * MODULE     : qt_view_widget.cpp
00004  * DESCRIPTION: QT View Widget class implementation
00005  * COPYRIGHT  : (C) 2008  Massimiliano Gubinelli
00006  *******************************************************************************
00007  * This software falls under the GNU general public license version 3 or later.
00008  * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009  * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010  ******************************************************************************/
00011 
00012 #include "qt_view_widget.hpp"
00013 #include "qt_widget.hpp"
00014 #include "qt_window_widget.hpp"
00015 #include "qt_utilities.hpp"
00016 #include "qt_renderer.hpp"
00017 
00018 #include "message.hpp"
00019 #include "converter.hpp"
00020 
00021 #include "QTMWidget.hpp"
00022 #include "QTMWindow.hpp"
00023 #include "QTMStyle.hpp"
00024 
00025 
00026 /******************************************************************************
00027  * Policy: qt_view_widget_rep owns the QWidget
00028  ******************************************************************************/
00029 
00034 qt_view_widget_rep::qt_view_widget_rep (QWidget* v)
00035  : qt_widget_rep(), view(v), current_renderer(NULL)  {}
00036 
00045 qt_view_widget_rep::~qt_view_widget_rep() {
00046   if (view) // this is redundant: delete does nothing if view==NULL
00047     delete view;
00048   if (DEBUG_QT)
00049     cout << "qt_view_widget_rep::~qt_view_widget_rep()\n";
00050 }
00051 
00052 
00053 void
00054 qt_view_widget_rep::send (slot s, blackbox val) {
00055   if (DEBUG_QT)
00056     cout << "qt_view_widget_rep::send " << slot_name (s) << LF;
00057 
00058   switch (s) {
00059     case SLOT_NAME:
00060     {   
00061       check_type<string> (val, "SLOT_NAME");
00062       string name = open_box<string> (val);
00063       view->window() -> setWindowTitle (to_qstring (tm_var_encode(name)));
00064     }
00065       break;
00066 
00067     case SLOT_INVALIDATE:
00068     {
00069       TYPE_CHECK (type_box (val) == type_helper<coord4>::id);
00070       coord4 p= open_box<coord4> (val);
00071       if (DEBUG_QT)
00072         cout << "Invalidating rect " << rectangle(p.x1,p.x2,p.x3,p.x4) << LF;
00073       qt_renderer_rep* ren = (qt_renderer_rep*)get_renderer (this);
00074       QTMWidget *canvas = qobject_cast <QTMWidget*>(view);
00075       if (ren && canvas) {
00076         SI x1 = p.x1, y1 = p.x2, x2 = p.x3, y2 = p.x4;    
00077         ren->outer_round (x1, y1, x2, y2);
00078         ren->decode (x1, y1);
00079         ren->decode (x2, y2);
00080         canvas->invalidate_rect (x1,y2,x2,y1);
00081       }
00082     }
00083       break;
00084     case SLOT_INVALIDATE_ALL:
00085     {
00086       ASSERT (is_nil (val), "type mismatch");
00087       if (DEBUG_QT)
00088         cout << "Invalidating all"<<  LF;
00089       QTMWidget *canvas = qobject_cast <QTMWidget*>(view);
00090       if (canvas) canvas->invalidate_all ();
00091     }
00092       break;
00093 
00094     case SLOT_MOUSE_GRAB:
00095       NOT_IMPLEMENTED;
00096       //send_mouse_grab (THIS, val);
00097       break;
00098       
00099     case SLOT_MOUSE_POINTER:
00100       NOT_IMPLEMENTED;
00101       //send_mouse_pointer (THIS, val);
00102       break;
00103       
00104     case SLOT_KEYBOARD_FOCUS:
00105       //send_keyboard_focus (THIS, val);
00106       TYPE_CHECK (type_box (val) == type_helper<bool>::id);
00107       if (open_box<bool> (val)) 
00108         the_keyboard_focus = this;
00109       if (DEBUG_QT)
00110         cout << "Ignored!\n";
00111       break;
00112     case SLOT_REFRESH:
00113       // This message is sent to refresh special widgets. Usually we just ignore it.
00114       break;
00115     default:
00116       FAILED ("unhandled slot type");
00117   }
00118 }
00119 
00123 blackbox
00124 qt_view_widget_rep::query (slot s, int type_id) {
00125   if ((DEBUG_QT) && (s != SLOT_RENDERER))
00126     cout << "qt_view_widget_rep::query " << slot_name(s) << LF;
00127   
00128   switch (s) {
00129     case SLOT_IDENTIFIER:
00130       TYPE_CHECK (type_id == type_helper<int>::id);
00131         // return close_box<int> ((int)view->window());
00132         // we need only to know if the widget is attached to some gui window
00133       return close_box<int> (view->window() ? 1 : 0);
00134 #if 0
00135     case SLOT_RENDERER:
00136     {
00137       TYPE_CHECK (type_id == type_helper<renderer>::id);
00138       renderer r = get_current_renderer();
00139         //FIXME: sometimes the renderer is queried outside repaint events 
00140         //       (see e.g. edit_interface_rep::idle_time)
00141         //       TeXmacs current policy is that we should return NULL only 
00142         //       if the widget is not attached (in X11 sense)
00143       if (!r) 
00144         r = the_qt_renderer();
00145       
00146       QTMWidget *canvas = qobject_cast <QTMWidget*>(view);
00147       if (r && canvas) {
00148         SI ox = -canvas->backing_pos.x()*PIXEL;
00149         SI oy = canvas->backing_pos.y()*PIXEL;
00150         r->set_origin(ox,oy);
00151       }
00152       
00153       return close_box<renderer> (r);
00154     }      
00155 #endif
00156       
00157     case SLOT_POSITION:
00158     {
00159       typedef pair<SI,SI> coord2;
00160       TYPE_CHECK (type_id == type_helper<coord2>::id);
00161       QPoint pt= view->pos();
00162       if (DEBUG_QT)
00163         cout << "Position (" << pt.x() << "," << pt.y() << ")\n";
00164       return close_box<coord2> (from_qpoint (pt));
00165     }
00166 
00167     case SLOT_SIZE:
00168     {
00169       typedef pair<SI,SI> coord2;
00170       TYPE_CHECK (type_id == type_helper<coord2>::id);
00171       QSize s= view->size();
00172       return close_box<coord2> (from_qsize (s));
00173     }
00174       
00175     default:
00176       FAILED ("cannot handle slot type");
00177       return blackbox ();
00178   }
00179 }
00180 
00185 void
00186 qt_view_widget_rep::notify (slot s, blackbox new_val) {
00187   if (DEBUG_QT)
00188     cout << "qt_view_widget_rep::notify " << slot_name(s) << LF;
00189   qt_widget_rep::notify (s, new_val);
00190 }
00191 
00195 widget
00196 qt_view_widget_rep::read (slot s, blackbox index) {
00197   if (DEBUG_QT)
00198     cout << "qt_view_widget_rep::read " << slot_name(s) << LF;
00199   
00200   switch (s) {
00201     case SLOT_WINDOW:
00202     {
00203       check_type_void (index, "SLOT_WINDOW");
00204       QWidget* qwin = view->window();
00205       QVariant v= qwin->property ("texmacs_window_widget");
00206       if (v.canConvert<void*> ())
00207         return (widget_rep*) (v.value<void*> ());
00208       else FAILED ("QWidget property not set");
00209     }
00210       break; // not reached     
00211     default:
00212       FAILED ("cannot handle slot type");
00213       return widget();
00214   }
00215 }
00216 
00220 void
00221 qt_view_widget_rep::write (slot s, blackbox index, widget w) {
00222   (void) index; (void) w;
00223   if (DEBUG_QT)
00224     cout << "qt_view_widget_rep::write " << slot_name (s) << LF;
00225   switch (s) {
00226     default:
00227       FAILED ("cannot handle slot type");
00228   }
00229 }
00230 
00231 
00236 widget
00237 qt_view_widget_rep::plain_window_widget (string _title, command q) {
00238   view->setWindowTitle (to_qstring (_title));
00239   widget wid= tm_new<qt_window_widget_rep> (view, q);
00240     //FIXME: is this the right thing to do?
00241   return wid;
00242 }
00243 
00244