/** * Set the cursor index, changing the color of that option and resetting the previously set option's color. * <p> * The option list starts at index 0. * @param newCursorIndex */ void MenuView::setCursorIndex(uint16_t newCursorIndex) { if (!buttons.empty()) { buttons[getCursorIndex()]->setTextColor(getOptionTextColor()); } this->cursorIndex = newCursorIndex; if (!buttons.empty()) { buttons[getCursorIndex()]->setTextColor(cursorTextColor); } }
void WeatherGraph::mouseReleaseEvent(QMouseEvent *event) { if (select_start) { int _start = qMin(select_start,select_stop); select_stop = qMax(select_start,select_stop); select_start = _start; int f = getCursorIndex(select_start - POSITON_ORDONNE); int l = getCursorIndex(select_stop - POSITON_ORDONNE); firstIndex = f < 0 ? 0 : f; lastIndex = l < 0 ? valueList.count() - 1 : l; isZoomed = true; select_start = 0; select_stop = 0; update(); } }
/** * Move the cursor to the previous option, wrapping around if on first. * <p> * If decrementing the cursor causes the cursor to fall outside of the visible window of options, also decrement the * visible window of options. */ void MenuView::decrementCursor() { long newCursorIndex = getCursorIndex() - 1; if (newCursorIndex < 0) { newCursorIndex = buttons.size() - 1; } setCursorIndex(static_cast<uint16_t>(newCursorIndex)); if (isCursorOutsideWindow()) { decrementWindowTopIndex(); } sizeButtons(); }
/** * Delegates the key up event to the buttons that represent the options of the menu. * <p> * Internally: * Enter - invokes the mouse up callback for the button that is the current cursor position * Arrow Up - Move decrements the cursor * Arrow Down - Increments the cursor * @param event */ void MenuView::onKeyUp(SDL_Event event) { // Enter activates the internal button if (event.key.keysym.sym == SDLK_RETURN) { buttons[getCursorIndex()]->onMouseUp(event); // Decrement the Cursor on UP } else if (event.key.keysym.sym == SDLK_UP) { decrementCursor(); // Increment the Cursor on DOWN } else if (event.key.keysym.sym == SDLK_DOWN) { incrementCursor(); } BaseView::onKeyUp(event); }
void MenuView::addOption(std::string optionText, const std::function<void(SDL_Event)> &callBack) { SDL_Rect rect = {0, 0, 0, 0}; auto button = new ButtonView(nullptr, rect); button->setText(optionText); button->setFontPath(FONT_CELTIC_HAND); button->setFontSize(0); button->setOnMouseUpCallback(callBack); button->setColor(getOptionBackgroundColor()); button->setTextColor(getOptionTextColor()); button->setFontSize(getOptionFontSize()); buttons.push_back(button); sizeButtons(); setCursorIndex(getCursorIndex()); }
int main ( int argc, char* argv[] ) { DlList_T myList; // List buffer to keep track of inserting or appending DlList_T listBuff; myList = dll_create(); if( myList == 0 ) { fprintf( stderr, "Cannot create list!\n" ); return( 1 ); } listBuff = dll_create(); if( listBuff == 0 ) { fprintf( stderr, "Cannot create buffer!\n" ); return( 1 ); } // number of bytes for getline int nbytes = 80; // Set up a string to read user input and file char *str = NULL; // Set up name for saving files to char *saveFile = NULL; // Read in a file // Check to make sure there's arguments of the file to open if( argc == 2 ) { FILE* pFile = fopen(argv[1], "r"); if( !pFile ) { perror("open failed"); fprintf( stderr, "could not read file '%s'\n", argv[1] ); return( 1 ); } else { // The file has been opened. Go through // and fill the doubly linked list while( getline(&str, (size_t *) &nbytes, pFile) ) { // Kill the new line killNL(str); // Set up space char* toAdd = (char*)malloc( ( strlen(str) + 1 ) * sizeof(char) ); // Add the previous read in line to that space strcpy( toAdd, str ); // Add it to the list dll_append( myList, toAdd ); //Free 'str' free( str ); str = NULL; } fclose( pFile ); } } else { printf( "no file supplied\n" ); } // Keep track if we should stop or not bool running = true; // Keep track of changes bool buffChange = false; // Keeps track if we're appending or inserting bool grabText = false; char grabMode = 'a'; // Keeps track of the cursor movement void* lastData = NULL; while( running ) { // Read in the next line getline(&str, (size_t *) &nbytes, stdin); // Kill new line killNL(str); // We're either appending or inserting if( grabText ) { if( strlen( str ) == 1 && str[0] == '.' ) { // A single period was entered to stop the adding // Set grabText to false grabText = false; // If the mode was set to append if( grabMode == 'a' ) { // We go through and pop all the elements // off the buffer and append them to the list int index = 0; void* data = dll_pop( listBuff, 0 ); while( data != NULL ) { dll_append( myList, data ); index++; data = dll_pop( listBuff, 0 ); } } else if ( grabMode == 'i' ) { // We go through and pop all the elements // off the buffer and insert them to the list int index = 0; // Set the insert index as the current // pointer's index int insertIndex = getCursorIndex( myList ); void* data = dll_pop( listBuff, 0 ); while( data != NULL ) { dll_insert_at( myList, insertIndex, data ); index++; insertIndex++; data = dll_pop( listBuff, 0 ); } } } else { // Set up space char* toAdd = (char*)malloc( ( strlen(str) + 1 ) * sizeof(char) ); // Add the previous read in line to that space strcpy( toAdd, str ); // Add it to the list dll_append( listBuff, toAdd ); } } else if( str[0] == 'a' ) { grabText = true; grabMode = 'a'; buffChange = true; // Current line } else if( str[0] == '.' ) { // Print the index of the current line if( str[1] == '=' ) { printf( "%d\n", getCursorIndex( myList ) ); // Print the current line } else { printf( "%s\n", (char*)getCursorData( myList ) ); } // Advance cursor to the next line // String length of 1 means they hit enter } else if( strlen(str) == 0 || str[0] == '+' ) { // Try to advance the cursor void* data = dll_next( myList ); // If the cursor didn't advance, notify the user if( data == lastData ){ printf("?\n"); // Otherwise the cursor advanced, print the line } else { lastData = data; printf( "%s\n", (char*)data ); } // Advance the cursor to the previous line } else if( str[0] == '-' ) { // Try to advance the cursor void* data = dll_prev( myList ); // If the cursor didn't advance, notify the user if( data == lastData ){ printf("?\n"); // Otherwise the cursor advanced, print the line } else { lastData = data; printf( "%s\n", (char*)data ); } // Delete line } else if( str[0] == 'd' ) { if( dll_has_next( myList ) ){ int index = getCursorIndex( myList ); void* data = dll_pop( myList, index ); free( data ); buffChange = true; } } else if( str[0] == 'i' ) { grabText = true; grabMode = 'i'; buffChange = true; // Last element } else if( str[0] == '$' ) { // For both "$=" and "$" it prints a "?" upon // an empty list int size = dll_size( myList ); if( size == 0 ) { printf( "?\n" ); } else { // If the command was "$=" just print // the possition of the last element if( str[1] == '=' ) { printf( "%d\n", size ); // Otherwise move to the last position // and print the last element } else { dll_move_to( myList, size - 1 ); void* data = dll_get( myList, size - 1 ); printf( "%s\n", (char*)data ); } } // Print } else if( str[0] == 'p' ) { printList( myList ); // Save } else if( str[0] == 'w' ) { char grab[(strlen(str) + 1)]; // Check if a file name has beens pecified sscanf(str, "%*s %s", grab); // If the length is greater than '1' we set // it as the last acceptable saveFile if( strlen(grab) > 0 ) { if( saveFile != NULL ) { free( saveFile ); } saveFile = (char*)malloc( ( strlen(str) + 1 ) * sizeof(char) ); strcpy( saveFile, grab ); } // Check if a quit flag has been given bool quit = (str[1] == 'q'); if( saveFile == NULL || strlen(saveFile) <= 0 ) { // We don't have an acceptable file name to check continue; } // Open file to be saved FILE* pFile = fopen(saveFile, "w"); if( !pFile ) { perror("open failed: "); } else { printf( "file name: '%s'\n", saveFile ); // Go through each node of the doulby linked list int index = 0; void* data = dll_get( myList, index ); while( data != NULL ) { // Write to file fputs((char*)data, pFile); fputs("\n", pFile); index++; data = dll_get( myList, index ); } // Close fclose( pFile ); // If we want to quit afterwards if( quit ) { running = false; printf("\nBye\n"); } // reset the buffer flag buffChange = false; } // Soft quit } else if ( str[0] == 'q' ) { // If the buffer hasn't changed stop the program if( !buffChange ) { running = false; printf( "\nBye\n" ); // Otherwise warn that there's been changes } else { printf( "? buffer dirty.\n" ); } // Hard quit } else if ( str[0] == 'Q' ) { running = false; printf( "\nBye\n" ); } else { // Check if a number char* end; int checkNum = strtol( str, &end, 10 ); // If the conversion did not encounter a string // We know the number is good if( !*end ) { // If we can move to the requested index if( dll_move_to( myList, checkNum - 1 ) ) { // Print that index printf( "%s\n", (char*)getCursorData( myList ) ); } else { printf( "?\n" ); } } } // Free 'str' free( str ); // Set 'str' to NULL str = NULL; } // Destroy our list dll_destroy( myList ); // Destroy the buffer dll_destroy( listBuff ); }
/** * Determine if the cursor is currently outside of the visible options. * @return boolean */ bool MenuView::isCursorOutsideWindow() { return getCursorIndex() < getWindowTopIndex() || getCursorIndex() >= getWindowTopIndex() + getWindowSize(); }
void WeatherGraph::paintEvent(QPaintEvent *event) { double graphx = POSITON_ORDONNE; double largeur_graph = width() - POSITON_ORDONNE - 10; double incx; double minGraph = labelPrecison * (ceil(minValue / labelPrecison) - 1); double maxGraph = labelPrecison * ceil(maxValue / labelPrecison); QPainter painter(this); // painter.setRenderHint(QPainter::Antialiasing, true); painter.setPen(QPen(Qt::darkGreen, 0)); painter.drawLine(graphx, height() - 10, width() - 10, height() - 10); painter.drawLine(graphx, 10, 40, height() - 10); if (!valueList.isEmpty()) { QDateTime firstDateDisplayed = dateList.at(firstIndex); QDateTime lastDateDisplayed = dateList.at(lastIndex); int largeur_temps = firstDateDisplayed.secsTo(lastDateDisplayed); if (largeur_temps < 30 && !isZoomed) largeur_temps = 30; double pps = largeur_graph / largeur_temps; painter.setPen(QPen(Qt::darkGreen, 0, Qt::DotLine)); int d = maxGraph - minGraph; int h = (height() - 20); h /= d; for (int i = 0 ; i < maxGraph - minGraph; i += labelPrecison) { int l = height() - 10 - i*h; painter.drawLine(graphx, l, width() - 10, l); } painter.setPen(QPen(Qt::green, 0)); painter.drawText(5,15, valueFormat.arg(maxGraph)); painter.drawText(5,height() - 10, valueFormat.arg(minGraph)); QPoint p1 = QPoint( graphx - 1, height() - (int)( (valueList.first() - minGraph) / (maxGraph - minGraph) * (height() - 20) ) - 10 ); QPoint p2; QDateTime d1 = firstDateDisplayed; QDateTime d2; for (int i = firstIndex ; i <= lastIndex ; i++) { d2 = dateList.at(i); incx = d1.secsTo(d2) * pps; graphx += incx; d1 = d2; p2 = QPoint( (int)graphx, height() - (int)( (valueList.at(i) - minGraph) / (maxGraph - minGraph) * (height() - 20) ) - 10 ); painter.drawLine(p1, p2); p1 = p2; } painter.setPen(QPen(QColor(255,255,255,192))); if (mouseOver) { int pos_graph = mousex - POSITON_ORDONNE; int index = getCursorIndex(pos_graph); if (index >= 0 && index < valueList.count()) { QString temp = valueFormat.arg(valueList.at(index)); QString when = dateList.at(index).toString("hh:mm:ss"); QFontMetrics fm = painter.fontMetrics(); int w = fm.width(when); int texty; if (mousey - 2 > 2 * fm.height()) { texty = mousey - 2; } else { texty = mousey + 2 * fm.height(); } if (mousex + 2 + w < width()) { painter.drawText(mousex + 2, texty, temp); painter.drawText(mousex + 2, texty - fm.height(), when); } else { painter.drawText(mousex - 2 - fm.width(temp), texty, temp); painter.drawText(mousex - 2 - fm.width(when), texty - fm.height(), when); } painter.drawLine(mousex, 10, mousex, height() - 10); if (select_start) { select_stop = mousex; } } else { mouseOver = false; } } if (select_start) { painter.drawRect(qMin(select_start, select_stop), 10, qAbs(select_stop - select_start), height() - 20); } } }